您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關PHP如何操作數據庫,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
連接數據庫
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 '<h3>菜鳥教程 mysqli_fetch_array 測試<h3>'; 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 '<h3>菜鳥教程 MySQL WHERE 子句測試<h3>'; 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 '<h3>菜鳥教程 mysqli_fetch_array 測試<h3>'; 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如何操作數據庫就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。