1 / 21

PHP 與 MySQL 入門學習指南

PHP 與 MySQL 入門學習指南. 第 29 章 PHP 與 MySQL. 凱文瑞克 著. PHP 如何與 MySQL 連接. 流程如下所示 : 使用者在使用者的瀏覽器上向網頁伺服器下命令。 網頁伺服器收到瀏覽器端的請求。 網頁伺服器依據請求尋找伺服器上的網頁。 伺服器執行網頁內含的 PHP 程式碼。 PHP 程式碼透過內建的 MySQL API 存取後端資料庫伺服器。 PHP 取回後端資料庫伺服器的查詢結果。 網頁伺服器將查詢結果傳回使用者。. Apache. Client. MySQL.

keola
Télécharger la présentation

PHP 與 MySQL 入門學習指南

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP與MySQL入門學習指南 第 29 章 PHP 與 MySQL 凱文瑞克 著

  2. PHP 如何與 MySQL 連接 流程如下所示 : • 使用者在使用者的瀏覽器上向網頁伺服器下命令。 • 網頁伺服器收到瀏覽器端的請求。 • 網頁伺服器依據請求尋找伺服器上的網頁。 • 伺服器執行網頁內含的 PHP 程式碼。 • PHP 程式碼透過內建的 MySQL API 存取後端資料庫伺服器。 • PHP 取回後端資料庫伺服器的查詢結果。 • 網頁伺服器將查詢結果傳回使用者。

  3. Apache Client MySQL

  4. PHP 連接及執行 MySQL 的步驟(一) • 連線:mysql_connect • 建立資料庫:mysql_create_db • 查詢 $select=mysql_slect_db('test'); $sql="select * from customers"; $result=mysql_query ($sql)  ||  die ("query fail");

  5. PHP 連接及執行 MySQL 的步驟(二) • 錯誤處理 if ($result=mysql_query("$sql")) {            echo "顯示正確的訊息"; } else {            echo "Error:".mysql_errno().";錯誤訊息:".mysql_error(); }

  6. PHP的MySQL函數群(1) • mysql_close---關閉MySQL連線   範例: $link=mysql_connect(“o3.net”,”jollne”,”akd83k”);         mysql_close($link); 如果沒有傳入link變數,則以最後的link為主。也就是上例也可以寫成 $link=mysql_connect(“o3.net:6677”,”jollne”,”akd83k”);         mysql_close($link);

  7. PHP的MySQL函數群(2) • mysql_connect---開啟MySQL伺服器連線 例如: <? $link = mysql_connect ("kraemer", "marliesle", "secret") or die ("Could not connect");  print ("Connected successfully"); mysql_close ($link); ?> 例如: $link=mysql_connect(“database,o3.net:7070”,”guest”,”guest123”);

  8. PHP的MySQL函數群(3) • mysql_pconnect ---  開啟MySQL伺服器持續連線 mysql_pconnect()比mysql_connect()好用,兩者主要的差別為: 1.mysql_pconnect()會檢查是否有相同host、相同username與相同password的connection,如果有則不會再重覆開啟。 2.當程式執行完成後將不會關閉連結到MySQL伺服器,反而會保留下來給之後使用。mysql_close( )將無法關閉以mysql_pconnect( )所開啟的連結,當PHP程式執行完時,利用connection因為有上面介紹的這兩種特性,因此稱利用mysql_pconnect()開啟的connection為persistent connection。

  9. PHP的MySQL函數群(4) • mysql_select_db ---  選擇一個資料庫 mysql_select_db( )設定在伺服器上現行的資料庫,如果沒有指定 link_identifier,則假定是最後開啟的連結,如果無開啟的連結,此函式會試著去建立一個連結,並且使用它,就好像是呼叫 mysql_connect( )一樣。隨後呼叫 mysql_query( ) 都會在此資料庫上工作。 相容的 mysql_selectdb( ) 也可以使用。 例如: $link=mysql_pconnect();         mysql_select_db(“star”,”$link);

  10. PHP的MySQL函數群(5) • mysql_create_db ---  新增一個MySQL資料庫 <?php $link = mysql_pconnect ("kron", "jutta", "geheim") or die ("無法連接資料庫");   if (mysql_create_db ("my_db")) { print ("Database created successfully\n");  } else {  printf ("Error creating database: %s\n", mysql_error ());  } ?>

  11. PHP的MySQL函數群(6) • mysql_drop_db --- 刪除MySQL資料庫 mysql_drop_db( )試著從伺服器刪除一整個資料庫。刪除資料庫。成功傳回true,失敗傳回false。 相容的mysql_dropdb( )也可以使用。

  12. PHP的MySQL函數群(7) • mysql_fetch_array ---  取得查詢後的陣列結果 <?php mysql_connect($host,$user,$password);\ $uesult=mysql_db_query(“student”,”select*from table”); while($row=mysql_fetch_array($result)){     echo $row[“fullname”];     echo $row[“grade”]; } mysql_free_result($result); ?>

  13. PHP的MySQL函數群(8) echo "<PRE> blob: $meta->blob max_length: $meta->max_length multiple_key: $meta->multiple_key name: $meta->name not_null: $meta->not_null numeric: $meta->numeric primary_key: $meta->primary_key table: $meta->table type: $meta->type unique_key: $meta->unique_key unsigned: $meta->unsigned zerofill: $meta->zerofill </PRE>"; $i++; } mysql_free_result ($result); ?> • mysql_fetch_field ---  取得欄位資訊 <? mysql_connect ($host, $user, $password) or die ("Could not connect"); $result = mysql_db_query ("database", "select * from table") or die ("Query failed"); # get column metadata $i = 0; while ($i < mysql_num_fields ($result)) { echo "Information for column $i:<BR>\n"; $meta = mysql_fetch_field ($result); if (!$meta) { echo "No information available<BR>\n"; }

  14. PHP的MySQL函數群(9)

  15. PHP的MySQL函數群(10) • mysql_fetch_object ---  取得查詢後的物件結果 範例: … while($row=mysql_fetch_object($result)){ echo $row->user_id; echo $row->fullname; }

  16. PHP的MySQL函數群(11) • mysql_fetch_row ---  取得單列結果 • 傳回一陣列,此陣列相當於取得列,如果沒有更多列則傳回false。mysql_fetch_row( )從結果取得資料的一列,將列放入陣列中傳回,各個結果欄位儲存在陣列偏移量之中,偏移量的起始值為0。隨後呼叫mysql_fetch_row( )將傳回結果中的下一列,如果沒有更多列則傳回false。

  17. PHP的MySQL函數群(12) • mysql_field_type ---  取得指定欄位的型態 <? mysql_connect ("localhost:3306"); mysql_select_db ("wisconsin"); $result = mysql_query ("SELECT * FROM onek"); $fields = mysql_num_fields ($result); $rows = mysql_num_rows ($result); $i = 0; $table = mysql_field_table ($result, $i); echo "Your '".$table."' table has ".$fields." fields and ".$rows." records <BR>"; echo "The table has the following fields <BR>"; while ($i < $fields) { $type = mysql_field_type ($result, $i); $name = mysql_field_name ($result, $i); $len = mysql_field_len ($result, $i); $flags = mysql_field_flags ($result, $i); echo $type." ".$name." ".$len." ".$flags."<BR>"; $i++; } mysql_close(); ?>

  18. PHP的MySQL函數群(13) • mysql_tablename ---  取得表格名稱 <?     mysql_connect ("localhost:3306");      $result = mysql_list_tables ("wisconsin");      $i = 0;      while ($i < mysql_num_rows ($result)) {                $tb_names[$i] = mysql_tablename ($result, $i);                echo $tb_names[$i] . "<BR>";               $i++;    } ?>

  19. PHP的MySQL函數群(14) • mysql_query ---  送出MySQL查詢 <? $result = mysql_query ("SELECT * WHERE 1=1") or die ("Invalid query"); ?> 如果my_col在表格my_tb1中不是一個欄位,則以下的查詢是語義上有錯誤的,因此 mysql_query( )失敗且傳回FALSE。 範例 : <? $result = mysql_query ("SELECT my_col FROM my_tbl") or die ("Invalid query"); ?>

  20. PHP的MySQL函數群(15) • mysql_errno --- 從先前MySQL操作傳回錯誤訊息代號 範例 : <? mysql_connect("marliesle"); echo mysql_errno().": ".mysql_error()."<BR>"; mysql_select_db("nonexistentdb"); echo mysql_errno().": ".mysql_error()."<BR>"; $conn = mysql_query("SELECT * FROM nonexistenttable"); echo mysql_errno().": ".mysql_error()."<BR>"; ?>

  21. PHP的MySQL函數群(16) • mysql_error --- 從先前MySQL操作傳回錯誤訊息 範例: <? mysql_connect("marliesle"); echo mysql_errno().": ".mysql_error()."<BR>"; mysql_select_db("nonexistentdb"); echo mysql_errno().": ".mysql_error()."<BR>"; $conn = mysql_query("SELECT * FROM nonexistenttable"); echo mysql_errno().": ".mysql_error()."<BR>"; ?>

More Related