PHP connection MySQL Database

Source: Internet
Author: User
Tags dsn php database sql injection stmt

Method One: Common method (Process oriented)

First, I first make the following assumptions (also applicable with method two and method three)

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

Here 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 a 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 $row =mysql_fetch_row ($result);

The code comment already explains everything. But there are some additions to be made here.

① using the @ (Error control operator)before functions such as mysql_connect (), mysql_select_db (), can ignore the error message generated by the system, and then we use Die () to customize the error message;

② extract data when, in addition to the above mysql_fetch_row, common there are mysql_fetch_assoc and mysql_fetch_array, the specific differences please check PHP Manual;

③ for the return value of the mysql_query () function, if the executed statement has a return value (such as SELECT, SHOW, describe, and so on), the corresponding data is returned (on success) or FALSE (on failure), if the executed statement has no return value (such as Delete, DROP, INSERT, update, and so on), returns True (on success) or FALSE (on failure).

Method Two: Object-oriented approach

In fact, this method is very similar to the normal method, just replace the corresponding function with the 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 ();

Here is the mysqli, meaning that the MySQL extension, either through a process-oriented approach or object-oriented way to interact with the database, the only difference is to call the function (object method) in a different way.

Method Three: Pdo method

PDO is actually the abbreviation for PHP database objects, which is the PHP object in Chinese. It provides a unified way for PHP to interact with the database.

This is a popular way to connect a database at present. The advantage of this is that the basic operation of the database is the same as long as the data source is provided correctly. That is, the same piece of code can either interact with MySQL, interact with SQLite3, and, of course, interact with PostgreSQL, provided you have the right data source. Here's a look at the code that connects MySQL:

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

If it is SQLite3, use the following code directly:

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

If it is PostgreSQL, the following code can deal with:

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

After the successful connection with the database, the following is only necessary to get data from the database or insert update data, the instance code is as follows:

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

At this point, the article concludes. Of course, the above is simply a brief introduction of PHP and database related to the simplest operation, there are many other things such as inserting, sorting, prevent SQL injection and so on some aspects of knowledge need to learn deeply. I hope to have the opportunity to discuss with you and make progress together.

PHP connection MySQL Database

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.