PHP運算元據庫

2020-07-16 10:05:48
連線資料庫

error_reporting(0);
$dbhost = 'localhost';  // mysql伺服器主機地址
$dbuser = 'root';            // mysql使用者名稱
$dbpass = '123456';          // mysql使用者名稱密碼
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
    die('資料庫連線失敗');
}
echo '資料庫連線成功';

建立資料庫(推薦學習:《mysql視訊教學》

$sql = 'CREATE DATABASE RUNOOB';
$retval = mysqli_query($conn,$sql );
if(! $retval )
{
    die('資料庫建立失敗');
}
echo "資料庫建立成功";

刪除資料庫

$sql = 'DROP DATABASE RUNOOB';
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('資料庫刪除失敗');
}
echo "資料庫刪除成功";

選擇資料庫

mysqli_select_db($conn, 'RUNOOB' );

資料型別

數值、日期/時間、字串

數值型別
tinyint
smallint
medium
int/integer
bigint
float
double
decimal
日期/時間
date
time
datetime
timestamp
字串
char
varchar
tinyblob
tinytext
blob
text
mediumblob
mediumtext
longblob
longtext

建立資料表

$sql = "CREATE TABLE runoob_tbl( ".
        "runoob_id INT NOT NULL AUTO_INCREMENT, ".
        "runoob_title VARCHAR(100) NOT NULL, ".
        "runoob_author VARCHAR(40) NOT NULL, ".
        "submission_date DATE, ".
        "PRIMARY KEY ( runoob_id ))ENGINE=InnoDB DEFAULT CHARSET=utf8; ";
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('建立資料表失敗');
}
echo "建立資料表成功";

刪除資料表

$sql = "DROP TABLE runoob_tbl";
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('刪除資料表失敗');
}
echo "刪除資料表成功";

插入資料

// 設定編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");
 
$runoob_title = '學習 Python';
$runoob_author = 'RUNOOB.COM';
$submission_date = '2016-03-06';
 
$sql = "INSERT INTO runoob_tbl ".
        "(runoob_title,runoob_author, submission_date) ".
        "VALUES ".
        "('$runoob_title','$runoob_author','$submission_date')";
 
 
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('插入資料失敗');
}
echo "資料插入成功";

查詢資料

// 設定編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");
 
$sql = 'SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('無法讀取資料');
}
echo '<h2>菜鳥教學 mysqli_fetch_array 測試<h2>';
echo '<table border="1"><tr><td>教學 ID</td><td>標題</td><td>作者</td><td>提交日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['runoob_id']}</td> ".
         "<td>{$row['runoob_title']} </td> ".
         "<td>{$row['runoob_author']} </td> ".
         "<td>{$row['submission_date']} </td> ".
         "</tr>";
}
echo '</table>';

WHERE

// 設定編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");
 
// 讀取 runoob_author 為 RUNOOB.COM 的資料
$sql = 'SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl
        WHERE runoob_author="RUNOOB.COM"';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('無法讀取資料: ' . mysqli_error($conn));
}
echo '<h2>菜鳥教學 MySQL WHERE 子句測試<h2>';
echo '<table border="1"><tr><td>教學 ID</td><td>標題</td><td>作者</td><td>提交日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQL_ASSOC))
{
    echo "<tr><td> {$row['runoob_id']}</td> ".
         "<td>{$row['runoob_title']} </td> ".
         "<td>{$row['runoob_author']} </td> ".
         "<td>{$row['submission_date']} </td> ".
         "</tr>";
}
echo '</table>';
// 釋放記憶體
mysqli_free_result($retval);

UPDATE

// 設定編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");
 
$sql = 'UPDATE runoob_tbl
        SET runoob_title="學習 Python"
        WHERE runoob_id=3';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('無法更新資料: ' . mysqli_error($conn));
}
echo '資料更新成功!';

DELETE

// 設定編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");
 
$sql = 'DELETE FROM runoob_tbl
        WHERE runoob_id=3';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('無法刪除資料: ' . mysqli_error($conn));
}
echo '資料刪除成功!';

LINK

// 設定編碼,防止中文亂碼
mysqli_query($conn , "set names utf8");
 
$sql = 'SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl
        WHERE runoob_author LIKE "%COM"';
 
mysqli_select_db( $conn, 'RUNOOB' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
    die('無法讀取資料: ' . mysqli_error($conn));
}
echo '<h2>菜鳥教學 mysqli_fetch_array 測試<h2>';
echo '<table border="1"><tr><td>教學 ID</td><td>標題</td><td>作者</td><td>提交日期</td></tr>';
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
    echo "<tr><td> {$row['runoob_id']}</td> ".
         "<td>{$row['runoob_title']} </td> ".
         "<td>{$row['runoob_author']} </td> ".
         "<td>{$row['submission_date']} </td> ".
         "</tr>";
}
echo '</table>';

排序

$sql = 'SELECT runoob_id, runoob_title, 
        runoob_author, submission_date
        FROM runoob_tbl
        ORDER BY  submission_date ASC(DESC)';
升序(降序)
預設升序

以上就是PHP運算元據庫的詳細內容,更多請關注TW511.COM其它相關文章!