Home >Backend Development >PHP Tutorial >How to connect PHP mysqli to MySQL database_PHP tutorial

How to connect PHP mysqli to MySQL database_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:29:15852browse

Learning1. Enable PHP API support

(1) First modify your php.ini configuration file.
Find the following statement:
;extension=php_mysqli.dll
Modify it to:
extension=php_mysqli.dll

(2) Restart Apache/IIS and that’s it .

(3) Note: PHP requires a separate file to support this extension library. Generally, the php_mysqli.dll file can be found in the ext directory under the PHP directory (libmysqli.dll in PHP

2.PHP mysqli ID card

mysqli is the abbreviation of "MySQL, Improved", this extension is only available for PHP 5. It works with MySQL 4.1.1 and higher. The extension fully supports the authentication protocol adopted in MySQL 5.1 and also supports prepared statements and multi-statement APIs. In addition, the extension provides an advanced, object-oriented programming interface.

3. mysqli predefined class

mysqli

expresses the connection between PHP and MySQL databases.

Constructor

mysqli - Constructs a new PHP mysqli object

Method

autocommit - Turn on or off the automatic commit database option
change_user - change the user of the specified database connection
character_set_name - return the default character set of the database connection
close - close a previously opened connection
commit - commit the current Transaction
connect - Open a new connection to the MySQL database server
debug - Perform debugging operations
dump_debug_info - Get debugging information
get_client_info - Return the client version
get_host_info - Return the connection usage Type
get_server_info - Returns the version of the MySQL server
get_server_version - Returns the version of the MySQL server
init - Initializes the PHP mysqli object
info - Gets information about the most recently executed query
kill - Asks the server Stop a mysql thread
multi_query - execute multiple queries
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

property

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. Basic syntax

<ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><?</SPAN><SPAN class=tag-name>php</SPAN></FONT></STRONG><SPAN>   </SPAN></SPAN><LI class=""><SPAN>      </SPAN><LI class=alt><SPAN>    /* Connect to a MySQL server  连接数据库服务器 */   </SPAN><LI class=""><SPAN>    $</SPAN><SPAN class=attribute><FONT color=#ff0000>link</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>mysqli_connect</FONT></SPAN><SPAN>(   </SPAN></SPAN><LI class=alt><SPAN>                'localhost',  /* The host to connect to 连接MySQL地址 */   </SPAN><LI class=""><SPAN>                'user',      /* The user to connect as 连接MySQL用户名 */   </SPAN><LI class=alt><SPAN>                'password',  /* The password to use 连接MySQL密码 */   </SPAN><LI class=""><SPAN>                'world');    /* The default database to query 连接数据库名称*/   </SPAN><LI class=alt><SPAN>      </SPAN><LI class=""><SPAN>    if (!$link) {   </SPAN><LI class=alt><SPAN>       printf("Can't connect to MySQL Server. Errorcode: %s ", mysqli_connect_error());   </SPAN><LI class=""><SPAN>       exit;   </SPAN><LI class=alt><SPAN>    }   </SPAN><LI class=""><SPAN>      </SPAN><LI class=alt><SPAN>    /* Send a query to the server 向服务器发送查询请求*/   </SPAN><LI class=""><SPAN>    if ($</SPAN><SPAN class=attribute><FONT color=#ff0000>result</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>mysqli_query</FONT></SPAN><SPAN>($link, 'SELECT Name, Population FROM City ORDER BY Population DESC LIMIT 5')) {   </SPAN></SPAN><LI class=alt><SPAN>      </SPAN><LI class=""><SPAN>        print("Very large cities are: ");   </SPAN><LI class=alt><SPAN>      </SPAN><LI class=""><SPAN>        /* Fetch the results of the query 返回查询的结果 */   </SPAN><LI class=alt><SPAN>        while( $</SPAN><SPAN class=attribute><FONT color=#ff0000>row</FONT></SPAN><SPAN> = </SPAN><SPAN class=attribute-value><FONT color=#0000ff>mysqli_fetch_assoc</FONT></SPAN><SPAN>($result) ){   </SPAN></SPAN><LI class=""><SPAN>            printf("%s (%s) ", $row['Name'], $row['Population']);   </SPAN><LI class=alt><SPAN>        }   </SPAN><LI class=""><SPAN>      </SPAN><LI class=alt><SPAN>        /* Destroy the result set and free the memory used for it 结束查询释放内存 */   </SPAN><LI class=""><SPAN>        mysqli_free_result($result);   </SPAN><LI class=alt><SPAN>    }   </SPAN><LI class=""><SPAN>      </SPAN><LI class=alt><SPAN>    /* Close the connection 关闭连接*/   </SPAN><LI class=""><SPAN>    mysqli_close($link);   </SPAN><LI class=alt><SPAN>    </SPAN><SPAN class=tag><STRONG><FONT color=#006699>?></span></font></strong></span><span> </span>
</li></ol>

The solutions described above can help us easily solve the problem of PHP mysqli connecting to the database.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446385.htmlTechArticleLearning 1. Enable PHP API support (1) First modify your php.ini configuration file. Find the following statement: ;extension=php_mysqli.dll Change it to: extension=php_mysqli.dll...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn