PHP connection to MySQL database

Source: Internet
Author: User
Tags dsn php database what php

Now that you have read this article, you must know what PHP and MySQL are like. But why are you still reading this article? It may be that you used to copy and paste some code and didn't really understand the meaning of the Code; or you may have understood it before, but like me, you have not been in touch for a while and are unfamiliar with it; or, someone asks you a simple question like this, and you are already waiting to answer the question. You just found this article by searching on the Internet, so I recommend it to someone who is there... in any case, I have summarized three common PHP Connection Methods to connect to the MySQL database. I hope this will help you, and of course it will also be my own review summary.

Method 1: Common method (process-oriented)

First, let me make the following assumptions (also applicable to method 2 and method 3)

$username=your_name;$userpass=your_pass;$dbhost=localhost;$dbdatabase=your_database;

The following are the key steps:

1 // generate a connection 2 $ db_connect = mysql_connect ($ dbhost, $ username, $ userpass) or die ("Unable to connect to the MySQL! "); 3 4 // SELECT the database to operate 5 mysql_select_db ($ dbdatabase, $ db_connect); 6 7 // execute MySQL Statement 8 $ result = mysql_query (" SELECT id, name FROM user "); 9 10 // extract data 11 $ row = mysql_fetch_row ($ result );

Code comments have explained everything. However, some additional information is needed here.

① Used before functions such as mysql_connect () and mysql_select_db ()@ (Error control operator)You can ignore the error information generated by the system, and then use die () to customize the error information;

② When extracting data, apart from mysql_fetch_row above, mysql_fetch_assoc and mysql_fetch_array are common. For details about the differences, refer to PHP Manual;

③ For the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, SHOW, DESCRIBE, etc.), the corresponding data (when successful) or FALSE (when failed) is returned ); if the executed statement does not return values (such as DELETE, DROP, INSERT, and UPDATE), TRUE (when successful) or FALSE (when failed) is returned ).

 

Method 2: Object-oriented Method

In fact, this method is very similar to a common method, but the corresponding function is replaced with an object-oriented method, directly look at the code.

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

Mysqli is used here, which refers to mysql extension. It can be used to interact with databases through process-oriented or object-oriented methods, the only difference is that the method for calling a function (Object method) is different.

 

Method 3: PDO Method

PDO is the abbreviation of PHP Database Objects.PHP database objects. It provides a unified PHP-database interaction method.

This is a popular method for connecting to databases. Its advantage is that, as long as the data source is provided correctly, the basic operations on the database are the same. That is to say, the same code can interact with MySQL, SQLite3, or PostgreSQL, provided that you provide the correct data source. The following code connects to MySQL:

$dsn='mysql:host='.$dbhost.';dbname='.$dbdatabase.';'$dbh=new PDO($dsn,$username,$userpass);

For SQLite3, use the following code:

$dsn='sqlite3:"C:\sqlite\user.db"';$dbh=new PDO($dsn);

For PostgreSQL, the following code can handle:

$dsn='pgsql:host='.$dbhost.' port=5432 dbname='.$dbdatabase.' user='.$username.' password='.$userpass;$dbh=new PDO($dsn);

After successfully establishing a connection with the database, you only need to obtain data from the database or insert the updated data. The instance code is as follows:

$stmt=$dbh->query('SELECT id,name FROM user');$row=$stmt->fetch();

 

So far, the article has come to an end. Of course, the above is just a brief introduction of the simplest PHP database-related operations. There are still a lot of things to be learned in depth, such as inserting, sorting, and preventing SQL injection. We hope to have the opportunity to discuss and make progress together.

Related Article

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.