Basic php connection to mysql database and query data _ MySQL

Source: Internet
Author: User
Tags dsn php database
This article describes how to connect to the mysql database using php and how to query data. For more information, see connect to the database. There are three methods.

1. General method:

$ Con = mysql_connect ($ dbhostip, $ username, $ userpassword) or die ("Unable to connect to the MySQL! "); $ Db = mysql_select_db ($ dbdatabasename, $ con); // execute the statement $ qres = mysql_query (" SELECT id, GoodsName FROM user "); // extract a piece of data 11 $ 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 extracted result of mysql_fetch_row is that there is no field name in the query (that is, there is no key id, GoodsName, only value), such:

// The extracted result of mysql_fetch_assoc has a key value, for example:

// The extracted result of mysql_fetch_array has a key value, which is a combination of the preceding two types, for example:

Use @ (error control operator) before functions such as mysql_connect () and mysql_select_db () to ignore system errors. then we use die () to customize error messages;

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 ).

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

Mysqli is used here, which means mysql extension. it can be used to interact with the database through process-oriented or object-oriented methods.

3. PDO method

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

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.

Code for connecting 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.' password='.$userpass;$dbh=new PDO($dsn);

Operation:

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

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.