PHP16 PHP訪問MySQL

來源:互聯網
上載者:User

標籤: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。

完整資料庫訪問步驟
  1. 建立資料連線
  2. 構建SQL語句
  3. 擷取結果集
  4. 處理結果集
  5. 關閉結果集資源
  6. 關閉資料庫連接
  完整範例程式碼
  • 查詢
// 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實現以下用例:

  1. 實現登陸
  2. 實現添加賽程資訊
  3. 實現刪除賽程資訊
  4. 實現修改賽程資訊
  5. 實現賽程資訊的分頁顯示

PHP16 PHP訪問MySQL

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.