Php connection to mysql instance code _ PHP Tutorial

Source: Internet
Author: User
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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.