標籤:enc 登陸 insert 分頁顯示 roc DBName ring bsp 支援
學習要點
- PHP訪問MySQL配置
- PHP訪問MySQL函數介紹
- 足球賽程資訊管理
PHP訪問MySQL配置PHP.ini設定檔確認以下配置已經開啟
extension=php_mysql.dll PHP5之前訪問資料庫模組
extension=php_mysqli.dll PHP5之後訪問資料庫模組
通過getinfo()獲得支援資訊確認PHP是否已支援訪問MySQL
PHP訪問MySQL函數介紹mysqli_connect() 函數
開啟一個到 MySQL 伺服器的新的串連。
mysqli_connect(host,username,password,dbname,port,socket);
參數 |
描述 |
host |
可選。規定主機名稱或 IP 位址。 |
username |
可選。規定 MySQL 使用者名稱。 |
password |
可選。規定 MySQL 密碼。 |
dbname |
可選。規定預設使用的資料庫。 |
port |
可選。規定嘗試串連到 MySQL 伺服器的連接埠號碼。 |
socket |
可選。規定 socket 或要使用的已命名 pipe。 |
返回一個代表到 MySQL 伺服器的串連的對象,資源類型。
$link=mysqli_connect(‘localhost‘,‘root‘,‘rootkit‘,‘myschool‘);
說明:mysql的預設連接埠3306,主機地址可以省略連接埠號碼,或者省略連接埠號碼參數。所以也可以寫成如下形式。
mysqli_query() 函數
執行某個針對資料庫的查詢。
mysqli_query(connection,query,resultmode);
參數 |
描述 |
connection |
必需。規定要使用的 MySQL 串連。 |
query |
必需,規定查詢字串。 |
resultmode |
可選。一個常量。可以是下列值中的任意一個: ü MYSQLI_USE_RESULT(如果需要檢索大量資料,請使用這個) ü MYSQLI_STORE_RESULT(預設) |
針對成功的 SELECT、SHOW、DESCRIBE 或 EXPLAIN 查詢,將返回一個 mysqli_result 對象。針對其他成功的查詢,將返回 TRUE。如果失敗,則返回 FALSE。
特別說明:在 PHP 5.3.0 中新增了非同步查詢的功能。
//構建SQL語句字串$query="SELECT * FROM student";//執行SQL語句並返回數組結果集$result=mysqli_query($link, $query);
mysqli_num_rows() 函數
返回結果集中行的數量。
mysqli_num_rows(result);
參數 |
描述 |
result |
必需。規定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的結果集標識符。 |
返回結果集中行的數量。
//擷取記錄數$rownum=mysqli_num_rows($result);
mysqli_fetch_assoc()函數
Fetch a result row as an associative array。
array mysqli_fetch_assoc ( mysqli_result $result );
參數 |
描述 |
result |
Procedural style only: A result set identifier returned by mysqli_query(), mysqli_store_result() or mysqli_use_result(). |
Returns an associative array of strings representing the fetched row in the result set, where each key in the array represents the name of one of the result set‘s columns or NULL if there are no more rows in resultset.
If two or more columns of the result have the same field names, the last column will take precedence. To access the other column(s) of the same name, you either need to access the result with numeric indices by using mysqli_fetch_row() or add alias names.。
//從數組結果集中返回一行記錄,傳回值為關聯陣列結構//mysqli_fetch_object($result)返回對象$row=mysqli_fetch_assoc($result);echo $row[‘studentno‘].‘:‘;//通過資料庫欄位名確定元素值echo $row[‘studentname‘]."<br>"; ;
mysqli_free_result()函數
釋放結果結果集記憶體。
mysqli_free_result(result)
參數 |
描述 |
result |
必需。規定由 mysqli_query()、mysqli_store_result() 或 mysqli_use_result() 返回的結果集標識符。 |
沒有傳回值。
mysqli_close()函數
關閉先前開啟的資料庫連接。
mysqli_close(connection);
參數 |
描述 |
connection |
規定要關閉的 MySQL 串連。 |
如果成功則返回 TRUE,如果失敗則返回 FALSE。
完整資料庫訪問步驟
- 建立資料連線
- 構建SQL語句
- 擷取結果集
- 處理結果集
- 關閉結果集資源
- 關閉資料庫連接
完整範例程式碼
// 1、建立和資料庫的串連$link = mysqli_connect ( ‘127.0.0.1‘, ‘root‘, ‘rootkit‘, ‘football‘ );if ($link == null) {echo ‘資料庫連接建立失敗‘;}// 2、設計SQL語句$query = "SELECT * FROM teaminfo";// 3、查詢$result = mysqli_query ( $link, $query );// 4、處理結果集$rownum = mysqli_num_rows ( $result ); // 擷取結果集的行數for($i = 0; $i < $rownum; $i ++) {$row = mysqli_fetch_assoc ( $result );echo $row [‘id‘] . ‘===‘ . $row [‘teamname‘] . ‘<br>‘;}// 5、釋放結果集mysqli_free_result ( $result );// 6、關閉資料庫連接mysqli_close ( $link );
// 1、建立和資料庫的串連$link = mysqli_connect ( ‘127.0.0.1‘, ‘root‘, ‘rootkit‘, ‘football‘ );if ($link == null) {echo ‘資料庫連接建立失敗‘;}// 2、設計SQL語句$query = "INSERT INTO teaminfo(teamname) VALUES(‘廈門國貿‘)";// 3、查詢$result = mysqli_query ( $link, $query );// 4、處理結果集if ($result) {echo ‘添加球隊資訊成功!‘;} else {echo ‘添加球隊資訊失敗!‘;echo mysqli_error($link); // mysqli_free_result($result);// 6、關閉資料庫連接mysqli_close ( $link );
// 1、建立和資料庫的串連$link = mysqli_connect ( ‘127.0.0.1‘, ‘root‘, ‘rootkit‘, ‘football‘ );if ($link == null) {echo ‘資料庫連接建立失敗‘;}// 2、設計SQL語句$query = "UPDATE teaminfo SET teamname=‘廈門七匹狼‘ WHERE id=19";// 3、查詢$result = mysqli_query ( $link, $query );// 4、處理結果集if ($result) {echo ‘更新球隊資訊成功!‘;} else {echo ‘更新球隊資訊失敗!‘;echo mysqli_error($link);}// 5、釋放結果集// mysqli_free_result($result);// 6、關閉資料庫連接mysqli_close ( $link );
// 1、建立和資料庫的串連$link = mysqli_connect ( ‘127.0.0.1‘, ‘root‘, ‘rootkit‘, ‘football‘ );if ($link == null) {echo ‘資料庫連接建立失敗‘;}// 2、設計SQL語句$query = "DELETE FROM teaminfo WHERE id=17";// 3、查詢$result = mysqli_query ( $link, $query );// 4、處理結果集if ($result) {echo ‘刪除球隊資訊成功!‘;} else {echo ‘刪除球隊資訊失敗!‘;echo mysqli_error($link);}// 5、釋放結果集// mysqli_free_result($result);// 6、關閉資料庫連接mysqli_close ( $link );
上機練習:2018世界盃亞洲區賽程資訊管理
使用mysqli實現以下用例:
- 實現登陸
- 實現添加賽程資訊
- 實現刪除賽程資訊
- 實現修改賽程資訊
- 實現賽程資訊的分頁顯示
PHP16 PHP訪問MySQL