Connection to PHP database--mysql

Source: Internet
Author: User
Tags dsn php database stmt

Database--a container for organizing, storing, and managing data by structure

Commonly used functions, what function to use can be checked on the manual, very convenient, the main first will be some basic can

MySQL function-(MySQL seems to be deprecated, recommended to use MYSQLI or PDO):

1. Process oriented: Connect a MySQL server--mysql_connect ($servername, $username, $passwd);

For example:

//Generate a connection
$con = mysql_connect (' localhost ', ' root ', ' 123456 ');
if (! $con) {
Die (' Connection failed! Reason for failure: '. mysql_error ());
}
// Select a database to manipulate $dbdatabase
mysql_select_db ($dbdatabase, $con);
// execute MySQL statement / / extract data $ Row=mysql_fetch_row ($result);

Using the MYSQLI function

$server = ' localhost ';

$user = ' root ';

$password = ' 123456 ';

$con = Mysqli_connect ($server, $user, $password)

Or

Die (' Connection database failed, failure reason: '. Mysqli_connect_error ());

Attention:

① 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, in addition to the above mysql_fetch_row, there are MYSQL_FETCH_ASSOC and mysql_fetch_array, the specific differences please consult the 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).

2, Object-oriented method-This method is very similar to the common method, but the corresponding function is replaced by the object-oriented method.

$db =New Mysqli ($server,$user,$password, $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 the abbreviation for PHP database objects, which is the PHP db object . It provides a unified way for PHP to interact with various databases.

The advantage is that the basic operation of the remaining database is the same as long as the data source is provided correctly. That is, the same code can interact with MySQL, interact with SQLite3, and, of course, interact with PostgreSQL (provided you have the right data source).

To connect to the MySQL database:

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

If it is SQLite3, use the following code directly:

$dsn = ' sqlite3: ' D:\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 ();

Connection to PHP database--mysql

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.