PHP three ways to connect to MySQL database (MySQL, mysqli, PDO)

Source: Internet
Author: User
Tags php and mysql php mysql php mysql extension prepare sql error sql injection stmt

PHP and MySQL connection has three kinds of API interface, namely: PHP mysql extension, php mysqli extension, PHP data Object (PDO), below for the above three kinds of connection methods to do a summary, in order to choose the best solution under different scenarios.

PHP's MySQL extension is a design development that allows PHP applications to interact with the MySQL database in an early extension. The MySQL extension provides a process-oriented interface and is designed for MySQL4.1.3 or earlier versions. This extension, though, can interact with the MySQL4.1.3 or newer database server, but does not support some of the features provided by the late MySQL server. Because it is too old and unsafe, it has been completely replaced by the later mysqli.

PHP's mysqli extension, which we sometimes call MySQL enhanced extension, can be used for new advanced features in MySQL4.1.3 or later versions. Its features are: Object-oriented interface, prepared statement support, multi-statement execution support, transaction support, enhanced debugging ability, embedded service support, preprocessing method completely solves the problem of SQL injection. However, it also has the disadvantage of only supporting MySQL database. This is certainly the best option if you don't operate other databases.

PDO is the abbreviation for PHP Data objects and is a Database abstraction layer specification in PHP applications. PDO provides a unified API interface that allows your PHP application to not care about the type of database server system you want to connect to, that is, if you use the PDO API, you can seamlessly switch database servers whenever you need them, such as from Oracle to MySQL, Just need to modify very little PHP code. It functions similar to interfaces such as JDBC, ODBC, DBI, and so on. Similarly, it solves the SQL injection problem and has good security. However, he also has shortcomings, some multi-statement execution query is not supported (but this situation is very small).

Frustration has also made a list of comparisons between the three of them:

PHP mysqli Extensions Pdo MySQL Extension for PHP
Introduction of PHP Version 5.0 5.0 3.0 ago
Does the php5.x contain Is Is Is
MySQL Development status Active Active in the PHP5.3 Maintenance only
Recommended use in MySQL new project Recommendation-Preferred Suggestions Not recommended
Character set support for the API Is Is Whether
Support for server-side prepare statements Is Is Whether
Support for client Prepare statements Whether Is Whether
Stored Procedure Support Scenarios Is Is Whether
Multi-statement execution support scenarios Is Most Whether
Do you support all MySQL4.1 features above Is Most Whether

Judging from the official results, MSQLI is preferred, followed by PDO. Many of the results given by the "folk" are inclined to use PDO because they are not burdened with the advantages of cross-Library, but also have the characteristics of fast reading and writing speed.

1.PHP with MySQL extension (this extension has been deprecated since PHP 5.5.0, and will be removed in the future), the PHP native way to connect to the database is process-oriented

<?php
$mysql _conf = Array (
' Host ' = ' 127.0.0.1:3306 ',
' db ' = ' Test ',
' Db_user ' = ' root ',
' Db_pwd ' = ' root ',
);
$mysql _conn = @mysql_connect ($mysql _conf[' host '], $mysql _conf[' Db_user '), $mysql _conf[' db_pwd ']);
if (! $mysql _conn) {
Die ("Could does connect to the database:\n". Mysql_error ());//Diagnostic connection Error
}
mysql_query ("Set names ' UTF8 '");//Code conversion
$select _db = mysql_select_db ($mysql _conf[' db ');
if (! $select _db) {
Die ("Could does connect to the db:\n". Mysql_error ());
}
$sql = "SELECT * from user;";
$res = mysql_query ($sql);
if (! $res) {
Die ("could get the res:\n". Mysql_error ());
}

while ($row = Mysql_fetch_assoc ($res)) {
Print_r ($row);
}

Mysql_close ($mysql _conn);
?>

2.PHP and mysqli extension, process-oriented, object

<?php
$mysql _conf = Array (
' Host ' = ' 127.0.0.1:3306 ',
' db ' = ' Test ',
' Db_user ' = ' root ',
' Db_pwd ' = ' joshua317 ',
);

$mysqli = @new mysqli ($mysql _conf[' host '], $mysql _conf[' Db_user '), $mysql _conf[' db_pwd ']);
if ($mysqli->connect_errno) {
Die ("Could does connect to the database:\n". $mysqli->connect_error);//Diagnostics Connection Error
}
$mysqli->query ("Set names ' UTF8 ';"); /Code Conversion
$select _db = $mysqli->select_db ($mysql _conf[' db ']);
if (! $select _db) {
Die ("Could does connect to the db:\n". $mysqli->error);
} $sql = "Select uid from user where name = ' Joshua ';";
$res = $mysqli->query ($sql);
if (! $res) {
Die ("SQL error:\n". $mysqli->error);
}
while ($row = $res->fetch_assoc ()) {
Var_dump ($row);
}

$res->free ();
$mysqli->close ();
?>

2.PHP and PDO extensions, process-oriented, object

<?php
$mysql _conf = Array (
' Host ' = ' 127.0.0.1:3306 ',
' db ' = ' Test ',
' Db_user ' = ' root ',
' Db_pwd ' = ' joshua317 ',
);
$pdo = new PDO ("mysql:host=". $mysql _conf[' host '). ";d bname=". $mysql _conf[' db '], $mysql _conf[' Db_user '), $mysql _conf[' db_pwd ']);//Create a PDO object
$pdo->exec ("Set names ' UTF8 '");
$sql = "SELECT * from user where name =?";
$stmt = $pdo->prepare ($sql);
$stmt->bindvalue (1, ' Joshua ', PDO::P aram_str);
$rs = $stmt->execute ();
if ($rs) {
Pdo::fetch_assoc Associative Array Form
Pdo::fetch_num Numeric index Array form
while ($row = $stmt->fetch (PDO::FETCH_ASSOC)) {
Var_dump ($row);
}
}

$pdo = null;//Close Connection
?>

PHP three ways to connect to MySQL database (MySQL, mysqli, PDO)

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.