"Go" PHP connection MySQL Database

Source: Internet
Author: User
Tags dsn php and mysql php database postgresql stmt what php

Now that you see this article, you must know what PHP and MySQL are all about, I will not be wordy. But why do you continue to read this article? It may be that you used to copy and paste some code, and did not really understand the meaning of the code, but also you can understand, but like me, there is a period of no contact, unfamiliar; or, someone asked you a similar simple question, and you already disdain to answer, direct Internet search, just found this article, So I recommend to those who who who ... Anyway, here I summed up three common PHP connection MySQL database methods, I hope to help you, of course, as a review of my own summary.

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:

Generate a connection $db_connect=mysql_connect ($dbhost, $username, $userpass) or die ("Unable to connect to the mysql!"); /Select a database mysql_select_db ($dbdatabase, $db _connect) that requires operation,//execute the MySQL statement $result=mysql_query ("Select Id,name from User") ;//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.

$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 ();

  

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. Hope to have the opportunity to discuss with you and make progress together.

Transferred from: http://www.cnblogs.com/csensix/archive/2012/05/23/2515494.html

"Go" 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.