在學習1. 開啟PHP的API支持
(1)首先修改您的php.ini的配置文件。
查找下面的語句:
;extension=php_mysqli.dll
將其修改為:
extension=php_mysqli.dll
(2)重新啟動Apache/IIS,即可。
(3)說明:PHP需要單獨的文件來支持這個擴展庫,一般在PHP目錄下的ext目錄裡能找到php_mysqli.dll文件(PHP <= 5.0.2 中是 libmysqli.dll),當然,在PHP的配置文件當中要有正確指向ext的信息(extension_dir)。假若您的PHP沒有這個文件,您可以去下載PHP5的源碼包。另外,這個API擴展,只能在PHP5以上版本使用。其它具體信息,請看下面。
2.PHP mysqli身份證
mysqli是“MySQL, Improved”的縮寫,該擴展僅適用於PHP 5。它能用於MySQL 4.1.1和更高版本。該擴展完全支持MySQL 5.1中采用的鑒定協議,也支持預處理語句和多語句API。此外,該擴展還提供了先進的、面向對象的編程接口。
3. mysqli預定義類
mysqli
表達了 PHP 和 MySQL 數據庫之間的連接。
構造函數
mysqli - 構造一個新的PHP mysqli對象
方法
autocommit - 打開或關閉自動提交的數據庫選項
change_user - 改變指定的數據庫連接的用戶
character_set_name - 返回數據庫連接的默認字符集
close - 關閉一個之前打開的連接
commit - 提交當前事務
connect - 打開一個到 MySQL 數據庫服務器的新連接
debug - 執行排錯操作
dump_debug_info - 取得排錯信息
get_client_info - 返回客戶端版本
get_host_info - 返回連接使用的類型
get_server_info - 返回 MySQL 服務器的版本
get_server_version - 返回 MySQL 服務器的版本
init - 初始化PHP mysqli對象
info - 取得最近執行的查詢的信息
kill - 要求服務器停止一個 mysql 線程
multi_query - 執行多個查詢
more_results - check if more results exist from currently executed multi-query
next_result - reads next result from currently executed multi-query
options - set options
ping - pings a server connection or reconnects if there is no connection
prepare - prepares a SQL query
query - performs a query
real_connect - attempts to open a connection to MySQL database server
escape_string - escapes special characters in a string for use in a SQL statement, taking into account the current charset of the connection
rollback - rolls back the current transaction
select_db - selects the default database
set_charset - sets the default client character set
ssl_set - sets ssl parameters
stat - gets the current system status
stmt_init- initializes a statement for use with mysqli_stmt_prepare
store_result - transfers a resultset from last query
thread_safe - returns whether thread safety is given or not
use_result - transfers an unbuffered resultset from last query
屬性
affected_rows - gets the number of affected rows in a previous MySQL operation
client_info - returns the MySQL client version as a string
client_version - returns the MySQL client version as an integer
errno - returns the error code for the most recent function call
error - returns the error string for the most recent function call
field_count - returns the number of columns for the most recent query
host_info - returns a string representing the type of connection used
info - retrieves information about the most recently executed query
insert_id - returns the auto generated id used in the last query
protocol_version - returns the version of the MySQL protocol used
server_info - returns a string that represents the server version number
server_version - returns the version number of the server as an integer
sqlstate - returns a string containing the SQLSTATE error code for the last error
thread_id - returns the thread ID for the current connection
warning_count - returns the number of warnings generated during execution of the previous SQL statement
4. 基本語法
- <?php
- /* Connect to a MySQL server 連接數據庫服務器 */
- $link = mysqli_connect(
- 'localhost', /* The host to connect to 連接MySQL地址 */
- 'user', /* The user to connect as 連接MySQL用戶名 */
- 'password', /* The password to use 連接MySQL密碼 */
- 'world'); /* The default database to query 連接數據庫名稱*/
- if (!$link) {
- printf("Can't connect to MySQL Server. Errorcode: %s ", 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: ");
- /* Fetch the results of the query 返回查詢的結果 */
- while( $row = mysqli_fetch_assoc($result) ){
- printf("%s (%s) ", $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);
- ?>
以上所描寫的解決辦法能夠幫助我們輕松解決PHP mysqli連接數據庫的問題。