PHP based connection Mysql database and query data _mysql

Source: Internet
Author: User
Tags dsn php database postgresql stmt

Three ways to connect to a database

1. General mode:

$con =mysql_connect ($dbhostip, $username, $userpassword) or die ("Unable to connect to the mysql!");
$db = mysql_select_db ($dbdatabasename, $con);
Executes
the statement $qres =mysql_query ("SELECT id,goodsname from User");
Extract a data one
$row =mysql_fetch_row ($result);//mysql_fetch_row can only extract the first record of the query result
//Extract multiple records
$reslist = Array ( );
$i =0;
while ($row = Mysql_fetch_row ($res)) {
   $reslist [$i] = $row;
   $i + +;
  }
Mysql_close ($con);

The result of mysql_fetch_row extraction is that there are no field names in the query (that is, no key id,goodsname, only values), as shown in the following figure:

The result of MYSQL_FETCH_ASSOC extraction has a key value, as shown in the following figure:

The result of mysql_fetch_array extraction is a key value, which is the first two kinds of synthesis, the following figure:

Using the @ (Error control operator) before mysql_connect (), mysql_select_db (), and so on, you can ignore the error message from the system, and then we use Die () to customize the error message;

For the return value of the mysql_query () function, if the executed statement has return values (such as SELECT, Show, describe, and so on), the corresponding data is returned (when it succeeds) or false (when it fails), if the executed statement does not have a return value (such as Delete, DROP, INSERT, update, and so on), returns True (when it succeeds) or false (when it fails).

2. Object-Oriented form

$db =new mysqli ($dbhostip, $username, $userpassword, $dbdatabasename);

if (Mysqli_connect_error ()) {  

echo ' could not connect to database. '  

Exit;

}

$result = $db->query ("Select Id,goodsname from User");

$row = $result->fetch_row ();

Here's the mysqli, which means MySQL is extended to interact with the database either through a process-oriented approach or through an object-oriented approach.

3. Pdo method

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

Its advantage is that as long as the data source is provided correctly, the rest of the basic operation of the database is the same. That is, the same piece of code can interact with MySQL, interact with SQLite3, and, of course, interact with PostgreSQL, provided you provide the right data source.

Code to connect to MySQL:

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

SQLite3:

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

PostgreSQL:
$dsn = ' pgsql:host= '. $dbhost. ' port=5432 dbname= '. $dbdatabase. ' user= '. $ $username. $ Userpass;
$DBH =new PDO ($DSN);

Operation:

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

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.