簡單介紹下 PHP5 中引入的 MYSQLI的用途_php基礎

來源:互聯網
上載者:User
在新下載的PHP5中你會發現多了一個mysqli.dll,它是幹什麼用的呢?我簡單介紹下。。。
mysqli.dll是PHP對mysql新特性的一個擴充支援。在PHP5中可以在php.ini中載入.
mysql後面的i,指improved, interface, ingenious, incompatible or incomplete(改擴充仍在開發中,因為MYSQL4。1和MYSQL5都沒有正式推出尚在開發中,新的特性沒有完全實現)
mysqli想實現的目標具體有:
-更簡單的維護
-更好的相容性
-向後相容
mysql(指PHP中的模組)發展到現在顯得比較淩亂,有必要重新做下整理。同時,有必要跟上MYSQL(DBMS)的發展步伐,加入新的特性的支援,以及適應MYSQL(DBMS)以後的版本。所以誕生了mysqli.dll
mysqli.dll的特性:
-可以和mysql.dll一樣的方式使用
-支援OO介面,簡簡單單調用
-支援MYSQL4。1引入的新特性
-通過mysqli_init() 等相關函數,可以設定進階串連選項
mysqli的使用例子:
1.和以前mysql.dll一樣的方法:
複製代碼 代碼如下:

<?php 
/* Connect to a MySQL server */  
$link = mysqli_connect(  
           'localhost',  /* The host to connect to */  
           'user',       /* The user to connect as */  
           'password',   /* The password to use */  
           'world');     /* The default table to query */ 
if (!$link) {  
  printf("Can't connect to MySQL Server. Errorcode: %sn", mysqli_connect_error());  
  exit;  
} 
/* Send a query to the server */  
if ($result = mysqli_query($link, 'SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) { 
print("Very large cities are:n"); 
/* Fetch the results of the query */  
   while( $row = mysqli_fetch_assoc($result) ){  
       printf("%s (%s)n", $row['Name'], $row['Population']);  
   } 
/* Destroy the result set and free the memory used for it */  
   mysqli_free_result($result);  
} 
/* Close the connection */  
mysqli_close($link);  
?>  

輸出結果:
Very large cities are:
Mumbai (Bombay) (10500000)
Seoul (9981619)
São Paulo (9968485)
Shanghai (9696300)
Jakarta (9604900)
2.使用內建OO介面方式調用:
複製代碼 代碼如下:

<?php 
/* Connect to a MySQL server */  
$mysqli = new mysqli('localhost', 'user', 'password', 'world'); 
if (mysqli_connect_errno()) {  
  printf("Can't connect to MySQL Server. Errorcode: %sn", mysqli_connect_error());  
  exit;  
} 
/* Send a query to the server */  
if ($result = $mysqli->query('SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) { 
print("Very large cities are:n"); 
/* Fetch the results of the query */  
   while( $row = $result->fetch_assoc() ){  
       printf("%s (%s)n", $row['Name'], $row['Population']);  
   } 
/* Destroy the result set and free the memory used for it */  
   $result->close();  
} 
/* Close the connection */  
$mysqli->close();  
?>  

支援的新特性還有:Bound Parameters,Bound Results等。。。
有興趣的可以直接去參看原英文:
http://www.zend.com/php5/articles/php5-mysqli.php#fn3
註:感覺這個不是對所有人都有用。不過。。。相信可以協助大家多瞭解些“變化”,能更好的把握“趨勢” 8-) 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.