Home  >  Article  >  Backend Development  >  Detailed explanation of PHP connection to mysql database test example_PHP tutorial

Detailed explanation of PHP connection to mysql database test example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:06:361140browse

If you want php to be able to connect to mysql on our homepage, you must know that php needs to enable the mysql module in php.ini. The specific opening method will be introduced later in the article. Let's look at the example test of php connecting to mysql.

PHP connects to the MySQL database through the mysql_connect() function to open a non-persistent MySQL connection.
Grammar:
mysql_connect(servername, username, password);

Parameter description:
servername: optional. The name of the server to be connected, the default is "localhost:3306", generally fill in localhost.
username: optional. The user name used to log in to the database server is usually root.
password: optional. Password to log in to the database server.


Example:

The code is as follows Copy code
 代码如下 复制代码

header("Content-type: text/html; charset=utf-8");
$link_id = @mysql_connect('localhost', 'root', '123456789');
if (!$link_id) {
die('连接服务器失败');
}
if (!@mysql_select_db('web', $link_id)) {
die('连接数据库失败');
}
if (!@mysql_query("set names 'utf8'", $link_id)) {
die('设置utf8格式失败');
}

mysql_close();
?>


 

header("Content-type: text/html; charset=utf-8");
$link_id = @mysql_connect('localhost', 'root', '123456789');
if (!$link_id) {

Die('Failed to connect to server');

}
if (!@mysql_select_db('web', $link_id)) {

Die('Failed to connect to database');
代码如下 复制代码

$link_id = @mysql_connect('localhost', 'root', '123456789');

}

if (!@mysql_query("set names 'utf8'", $link_id)) {

Die('Failed to set utf8 format');
代码如下 复制代码

if (!$link_id) {
die('连接服务器失败');
}

}

mysql_close();
代码如下 复制代码


if (!@mysql_select_db('web', $link_id)) {
die('连接数据库失败');
}

?>


 代码如下 复制代码


if (!@mysql_query("set names 'utf8'", $link_id)) {
    die('设置utf8格式失败');

Example analysis: header("Content-type: text/html; charset=utf-8"); Set the page content to html and the page encoding format to utf-8. Ensure that: 1. Database encoding 2. Page encoding 3. Connection encoding are consistent, and there will be no garbled characters.
The code is as follows Copy code
$link_id = @mysql_connect('localhost', 'root', '123456789');
Connect to the database, if successful, return a MySQL connection ID to $link_id, otherwise return FALSE. @ means not to output or display database error information to prevent leakage of website privacy.
The code is as follows Copy code
if (!$link_id) { Die('Failed to connect to server'); }
Determine whether the connection to the database server is successful. If not, the message "Connection to server failed" will be output and the execution of php will be terminated.
The code is as follows Copy code
if (!@mysql_select_db('web', $link_id)) { Die('Failed to connect to database'); }
Determine whether the connection to the server database is successful. If not, the message "Failed to connect to the database" will be output and the execution of php will be terminated.
The code is as follows Copy code
if (!@mysql_query("set names 'utf8'", $link_id)) { Die('Failed to set utf8 format'); }

Set the encoding for PHP to connect to the MySQL database. If unsuccessful, the message "Failed to set UTF8 format" will be output and the execution of PHP will be terminated.

Baidu found that the PHP+MYSQL environment is not configured properly, and the default mysql of php5 is closed
The code is as follows
 代码如下 复制代码

 mysql_close(); 

Copy code

mysql_close();

Release resources, that is: close the database.

Prompts for connecting to mysql database

Running code appears: Call to undefined function 'mysql_connect()'... failed
Copy the php_mysql.dll and libmysql.dll files to c:/winnt/system32 (I missed libmysql.dll)

Find ;extension=php_mysql in php.ini and remove the ";" in front of it Restart the server http://www.bkjia.com/PHPjc/630688.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/630688.html
TechArticle
If you want php to be able to connect to mysql on our homepage, you must know that php needs to enable the mysql module in php.ini Yes, the specific opening method will be introduced later in the article. Let’s look at the php connection my...
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