這篇文章將為大家詳細講解有關(guān)PHP如何操作數(shù)據(jù)庫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
從網(wǎng)站建設(shè)到定制行業(yè)解決方案,為提供成都網(wǎng)站建設(shè)、成都網(wǎng)站設(shè)計服務(wù)體系,各種行業(yè)企業(yè)客戶提供網(wǎng)站建設(shè)解決方案,助力業(yè)務(wù)快速發(fā)展。創(chuàng)新互聯(lián)將不斷加快創(chuàng)新步伐,提供優(yōu)質(zhì)的建站服務(wù)。
連接數(shù)據(jù)庫
error_reporting(0); $dbhost = 'localhost'; // MySQL服務(wù)器主機地址 $dbuser = 'root'; // mysql用戶名 $dbpass = '123456'; // mysql用戶名密碼 $conn = mysqli_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('數(shù)據(jù)庫連接失敗'); } echo '數(shù)據(jù)庫連接成功';
創(chuàng)建數(shù)據(jù)庫(推薦學(xué)習(xí):《mysql視頻教程》)
$sql = 'CREATE DATABASE RUNOOB'; $retval = mysqli_query($conn,$sql ); if(! $retval ) { die('數(shù)據(jù)庫創(chuàng)建失敗'); } echo "數(shù)據(jù)庫創(chuàng)建成功";
刪除數(shù)據(jù)庫
$sql = 'DROP DATABASE RUNOOB'; $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('數(shù)據(jù)庫刪除失敗'); } echo "數(shù)據(jù)庫刪除成功";
選擇數(shù)據(jù)庫
mysqli_select_db($conn, 'RUNOOB' );
數(shù)據(jù)類型
數(shù)值、日期/時間、字符串
數(shù)值類型 tinyint smallint medium int/integer bigint float double decimal 日期/時間 date time datetime timestamp 字符串 char varchar tinyblob tinytext blob text mediumblob mediumtext longblob longtext
創(chuàng)建數(shù)據(jù)表
$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('創(chuàng)建數(shù)據(jù)表失敗'); } echo "創(chuàng)建數(shù)據(jù)表成功";
刪除數(shù)據(jù)表
$sql = "DROP TABLE runoob_tbl"; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('刪除數(shù)據(jù)表失敗'); } echo "刪除數(shù)據(jù)表成功";
插入數(shù)據(jù)
// 設(shè)置編碼,防止中文亂碼 mysqli_query($conn , "set names utf8"); $runoob_title = '學(xué)習(xí) 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('插入數(shù)據(jù)失敗'); } echo "數(shù)據(jù)插入成功";
查詢數(shù)據(jù)
// 設(shè)置編碼,防止中文亂碼 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('無法讀取數(shù)據(jù)'); } echo '菜鳥教程 mysqli_fetch_array 測試
'; echo '
教程 ID | 標題 | 作者 | 提交日期 |
{$row['runoob_id']} | ". "{$row['runoob_title']} | ". "{$row['runoob_author']} | ". "{$row['submission_date']} | ". "
WHERE
// 設(shè)置編碼,防止中文亂碼 mysqli_query($conn , "set names utf8"); // 讀取 runoob_author 為 RUNOOB.COM 的數(shù)據(jù) $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('無法讀取數(shù)據(jù): ' . mysqli_error($conn)); } echo '菜鳥教程 MySQL WHERE 子句測試
'; echo '
教程 ID | 標題 | 作者 | 提交日期 |
{$row['runoob_id']} | ". "{$row['runoob_title']} | ". "{$row['runoob_author']} | ". "{$row['submission_date']} | ". "
UPDATE
// 設(shè)置編碼,防止中文亂碼 mysqli_query($conn , "set names utf8"); $sql = 'UPDATE runoob_tbl SET runoob_title="學(xué)習(xí) Python" WHERE runoob_id=3'; mysqli_select_db( $conn, 'RUNOOB' ); $retval = mysqli_query( $conn, $sql ); if(! $retval ) { die('無法更新數(shù)據(jù): ' . mysqli_error($conn)); } echo '數(shù)據(jù)更新成功!';
DELETE
// 設(shè)置編碼,防止中文亂碼 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('無法刪除數(shù)據(jù): ' . mysqli_error($conn)); } echo '數(shù)據(jù)刪除成功!';
LINK
// 設(shè)置編碼,防止中文亂碼 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('無法讀取數(shù)據(jù): ' . mysqli_error($conn)); } echo '菜鳥教程 mysqli_fetch_array 測試
'; echo '
教程 ID | 標題 | 作者 | 提交日期 |
{$row['runoob_id']} | ". "{$row['runoob_title']} | ". "{$row['runoob_author']} | ". "{$row['submission_date']} | ". "
排序
$sql = 'SELECT runoob_id, runoob_title, runoob_author, submission_date FROM runoob_tbl ORDER BY submission_date ASC(DESC)'; 升序(降序) 默認升序
關(guān)于PHP如何操作數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。