自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

PHP mysqli如何連接MySQL數(shù)據(jù)庫

開發(fā) 后端
PHP mysqli正確連接MySQL數(shù)據(jù)庫的首要操作就是需要開啟PHP的API支持,然后還需要將PHP mysqli預(yù)定義類,來實現(xiàn)我們的目的。

在學(xué)習(xí)PHP語言的時候,我們知道,在進(jìn)行MySQL數(shù)據(jù)庫連接的時候我們采用了PHP mysqli這一方法。但是在實際操作中會遇到一些問題。通過本文的介紹,我們將會學(xué)到如何正確的利用PHP mysqli連接數(shù)據(jù)庫。

#t#1. 開啟PHP的API支持

(1)首先修改您的php.ini的配置文件。
      查找下面的語句:
      ;extension=php_mysqli.dll
      將其修改為:
      extension=php_mysqli.dll

(2)重新啟動Apache/IIS,即可。

(3)說明:PHP需要單獨的文件來支持這個擴(kuò)展庫,一般在PHP目錄下的ext目錄里能找到php_mysqli.dll文件(PHP <= 5.0.2 中是 libmysqli.dll),當(dāng)然,在PHP的配置文件當(dāng)中要有正確指向ext的信息(extension_dir)。假若您的PHP沒有這個文件,您可以去下載PHP5的源碼包。另外,這個API擴(kuò)展,只能在PHP5以上版本使用。其它具體信息,請看下面。

2.PHP mysqli身份證

mysqli是“MySQL, Improved”的縮寫,該擴(kuò)展僅適用于PHP 5。它能用于MySQL 4.1.1和更高版本。該擴(kuò)展完全支持MySQL 5.1中采用的鑒定協(xié)議,也支持預(yù)處理語句和多語句API。此外,該擴(kuò)展還提供了先進(jìn)的、面向?qū)ο蟮木幊探涌凇?/P>

3. mysqli預(yù)定義類

mysqli

表達(dá)了 PHP 和 MySQL 數(shù)據(jù)庫之間的連接。 

構(gòu)造函數(shù)

mysqli - 構(gòu)造一個新的PHP mysqli對象

方法

autocommit - 打開或關(guān)閉自動提交的數(shù)據(jù)庫選項
change_user - 改變指定的數(shù)據(jù)庫連接的用戶
character_set_name - 返回數(shù)據(jù)庫連接的默認(rèn)字符集
close - 關(guān)閉一個之前打開的連接
commit - 提交當(dāng)前事務(wù)
connect - 打開一個到 MySQL 數(shù)據(jù)庫服務(wù)器的新連接
debug - 執(zhí)行排錯操作
dump_debug_info - 取得排錯信息
get_client_info - 返回客戶端版本
get_host_info - 返回連接使用的類型
get_server_info - 返回 MySQL 服務(wù)器的版本
get_server_version - 返回 MySQL 服務(wù)器的版本
init - 初始化PHP mysqli對象
info - 取得最近執(zhí)行的查詢的信息
kill - 要求服務(wù)器停止一個 mysql 線程
multi_query - 執(zhí)行多個查詢
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. 基本語法

  1. <?php   
  2.       
  3.     /* Connect to a MySQL server  連接數(shù)據(jù)庫服務(wù)器 */   
  4.     $link = mysqli_connect(   
  5.                 'localhost',  /* The host to connect to 連接MySQL地址 */   
  6.                 'user',      /* The user to connect as 連接MySQL用戶名 */   
  7.                 'password',  /* The password to use 連接MySQL密碼 */   
  8.                 'world');    /* The default database to query 連接數(shù)據(jù)庫名稱*/   
  9.       
  10.     if (!$link) {   
  11.        printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());   
  12.        exit;   
  13.     }   
  14.       
  15.     /* Send a query to the server 向服務(wù)器發(fā)送查詢請求*/   
  16.     if ($result = mysqli_query($link, 'SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) {   
  17.       
  18.         print("Very large cities are: ");   
  19.       
  20.         /* Fetch the results of the query 返回查詢的結(jié)果 */   
  21.         while( $row = mysqli_fetch_assoc($result) ){   
  22.             printf("%s (%s) ", $row['Name'], $row['Population']);   
  23.         }   
  24.       
  25.         /* Destroy the result set and free the memory used for it 結(jié)束查詢釋放內(nèi)存 */   
  26.         mysqli_free_result($result);   
  27.     }   
  28.       
  29.     /* Close the connection 關(guān)閉連接*/   
  30.     mysqli_close($link);   
  31.     ?> 

以上所描寫的解決辦法能夠幫助我們輕松解決PHP mysqli連接數(shù)據(jù)庫的問題。

責(zé)任編輯:曹凱 來源: 機(jī)械工業(yè)出版社
相關(guān)推薦

2010-05-27 10:10:00

連接MySQL數(shù)據(jù)庫

2010-05-14 11:12:16

連接MySql

2020-11-23 14:16:42

Golang

2021-08-02 10:53:28

PythonMySQL數(shù)據(jù)庫

2010-06-01 10:47:21

連接MySQL數(shù)據(jù)庫

2017-09-11 19:30:44

MySQLCmd命令連接數(shù)據(jù)庫

2018-02-26 20:00:00

編程語言JavaMySQL

2009-06-01 09:57:43

netbeans連接數(shù)netbeans數(shù)據(jù)庫netbeans連接m

2009-07-07 14:56:33

JSP連接MySQL

2011-05-26 13:42:50

MFC連接MySql數(shù)據(jù)庫

2020-09-22 15:56:31

Java

2009-11-30 17:54:56

PHP連接Sql數(shù)據(jù)庫

2009-12-08 17:23:12

PHP PDO類

2011-07-27 13:58:48

EclipseMySQL

2011-07-19 11:12:36

PHPMySQL數(shù)據(jù)庫

2017-11-27 11:41:06

python數(shù)據(jù)庫數(shù)據(jù)分析

2024-01-02 08:47:42

2010-05-28 10:34:39

連接MySQL數(shù)據(jù)庫

2010-06-07 15:24:34

Java連接MYSQL

2011-08-05 10:01:47

MySQL庫Pdo-MysqlMysqli
點贊
收藏

51CTO技術(shù)棧公眾號