PHP connection MySQL Multiple methods and instance code

Source: Internet
Author: User
    1. mysql_connect ("localhost", "root", "1981427")//Connect to server on localhost, user name is root
    2. ?>
Copy Code

Connect and select a database

    1. @mysql_select_db ("test")//Select Database MyDB
    2. ?>
Copy Code

This makes it possible to connect to the database.

Run code appears: Call to undefined function ' mysql_connect () ' ... Failure this prompt function undefined is said to have no mysql_connect () functions,

Workaround: Copy the Php_mysql.dll and Libmysql.dll files to the C:winntsystem32 (I missed the Libmysql.dll) and find the php.ini, and remove the ";" from the front. Restart Server example 1,conn.php file code:

    1. $conn = @mysql_connect ("localhost", "root", "root") or Die ("Database link error");
    2. mysql_select_db ("Data", $conn);//data is the database name
    3. mysql_query ("Set names ' GBK '"); Use GBK Chinese code;
    4. ?>
Copy Code

index.php File Code:

    1. Include ("conn.php");//introduction of conn.php File
    2. $sql = "SELECT * from ' table ' ORDER by id DESC";
    3. $query =mysql_query ($sql);
    4. while ($row =mysql_fetch_array ($query)) {
    5. ?>
Copy Code

Display data content:

    1. }
    2. ?>
Copy Code

PHP connection MySQL Database

Method One: Common method (Process oriented)

First, database user information:

    1. Generate a connection

    2. $db _connect=mysql_connect ($dbhost, $username, $userpass) or Die ("unable-to-connect to the mysql!");
    3. Select a database to operate on
    4. mysql_select_db ($dbdatabase, $db _connect);
    5. Execute MySQL Statement
    6. $result =mysql_query ("Select Id,name from user");

    7. Extracting data

    8. $row =mysql_fetch_row ($result);

Copy Code

Add: ① use @ (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, in addition to the above mysql_fetch_row, there are common 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: Php Object-oriented method to connect MySQL database

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. $result = $db->query ("Select Id,name from user");
    7. $row = $result->fetch_row ();
Copy Code

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: Php PDO method to connect MySQL database

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. Code to connect to MySQL:

    1. $dsn = ' mysql:host= '. $dbhost. '; Dbname= '. $dbdatabase. '; '

    2. $DBH =new PDO ($DSN, $username, $userpass);

    3. Sqlite3:

    4. $dsn = ' sqlite3: ' c:\sqlite\user.db ';
    5. $DBH =new PDO ($DSN);

    6. PostgreSQL

    7. $dsn = ' pgsql:host= '. $dbhost. ' port=5432 dbname= '. $dbdatabase. ' user= '. $username. ' password= '. $userpass;
    8. $DBH =new PDO ($DSN);
Copy Code

After a successful connection with the database, the following only needs to fetch data from the database or insert the update data.

SQL code:

    1. $stmt = $dbh->query (' Select id,name from user ');
    2. $row = $stmt->fetch ();
Copy Code
  • 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.