Php connection to the mysql instance code. This article uses the mysql_connect function of php to connect to the mysql database. it prompts you that your php has php_mysql.dll and libmysql. dll and php. in ini; extensionphp_mysql, remove the previous article "This is an article using the mysql_connect function of php to connect to the mysql database. the prompt conditions require your php to have php_mysql.dll and libmysql. dll and php. in ini; extension = php_mysql, remove the previous.
| The code is as follows: |
|
|
Mysql_connect ("localhost", "root", "1981427") // connect to the server on localhost. the username is root.
?> |
Connect and select database
| The code is as follows: |
|
|
@ Mysql_select_db ("test") // select the database mydb
?> |
In this way, we can connect to the database.
Possible problems
The following code appears: Call to undefined function 'MySQL _ connect ()'... Failed
This prompt function undefined indicates that the mysql_connect () function is not available,
The solution is as follows:
Copy the php_mysql.dll and libmysql. dll files to c: winntsystem32 (I missed libmysql. dll)
Find; extension = php_mysql in php. ini and remove ";" restart the server.
Now everything is ready. now we have a simple connection instance.
Example 1
The conn. php file code is as follows:
| The code is as follows: |
|
$ Conn = @ mysql_connect ("localhost", "root", "root") or die ("database link error ");
Mysql_select_db ("data", $ conn); // data is the database name
Mysql_query ("set names 'gbk'"); // use GBK Chinese encoding;
?>
|
The index. php file code is as follows:
| The code is as follows: |
|
|
Include ("conn. php"); // introduce the conn. php file
$ SQL = "SELECT * FROM 'table' order by id desc ";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
?>
Show data content:
}
?> |
In this way, we implement a simple php mysql connection and query the data we need.