How to connect PHP to MYSQL database

Source: Internet
Author: User
This article mainly introduces three common methods for connecting PHP to the MYSQL database. for beginners of PHP, we only need to master the basic operations such as writing, reading, editing, and deleting the database, you can also write simple programs, such as message books and news and article systems. Throughout the process, the connection to the MySQL database is also important. you can use multiple methods for connection. for new users, we do not need to analyze which method is used to optimize system resources, we can connect to it first.

Here, we will sort out several common methods for connecting PHP to the MYSQL database.

First,Common methods

$ Mysql_server = "localhost"; $ mysql_username = "database username"; $ mysql_password = "database password"; $ mysql_database = "database name "; // create a database connection $ conn = mysql_connect ($ mysql_server, $ mysql_username, $ mysql_password) or die ("database link error"); // select a database mysql_select_db ($ mysql_database, $ conn); mysql_query ("set names 'utf8'"); // execute the MySQL statement $ result = mysql_query ("SELECT id, name FROM database table "); // Extract Data $ row = mysql_fetch_row ($ result );

When extracting data, we use mysql_fetch_row and mysql_fetch_assoc and mysql_fetch_array. for details, refer to the manual.

Second,Object-oriented method

$db=new mysqli($dbhost,$username,$userpass,$dbdatabase);if(mysqli_connect_error()){echo 'Could not connect to database.';exit;}$result=$db->query("SELECT id,name FROM user");$row=$result->fetch_row();

Third,PDO method

$dsn='mysql:host='.$dbhost.';dbname='.$dbdatabase.';'$dbh=new PDO($dsn,$username,$userpass);$stmt=$dbh->query('SELECT id,name FROM user');$row=$stmt->fetch();

The above are three common methods for connecting to the MYSQL database using PHP. we can try to use it. generally, we use the first method to connect to the MYSQL database.

The above is all the content of this article. I hope it will help you learn and support PHP.

For more articles about how to connect PHP to the MYSQL database, refer to PHP Chinese network!

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.