We look at the PHP connection database, in the end what way AH solution ideas

Source: Internet
Author: User
Tags informix stmt
Everyone See PHP Connection database, in the end what way ah?
Http://www.44lanweb.com/lan_server/php/php_mysql_connect.aspx
It is said to use mysql_connect () to connect, I remember the teacher said it is best to use PDO or what ADO connected what, remember is not very clear, please ask the next PHP connection database what connection ah?

------Solution--------------------
The most common is to use the mysql_connect () function to connect

PDO seems to block out the differences between the various databases, providing a unified interface, seemingly

------Solution--------------------
Database of the difference, the general PHP is with MySQL, so use mysql_connect more, but if the database is access, MSSQL should consider the use of ADO or OLE DB such links.
The above is a personal understanding, for reference only, hehe ~ ~
------Solution--------------------
The best teacher for this problem is Uncle Google. Additional PDO extension support is required. But it seems like where I saw the new version will be directly supported. Or I remember wrong, because I didn't use PDO so others need to be supplemented.

=================

Php-mysql is the most primitive PHP operation MySQL Extension, php-mysqli I for improvement, the more advanced phase of the function, in Extension, itself also increased security. The PDO (PHP Data Object) is provided with a abstraction Layer to manipulate the repository, it is not really see what is the difference, so just read the program directly ...

First, let's take a look at a code written in Php-mysql, a sample of which is commonly used around the world:


Mysql_connect ($db _host, $db _user, $db _password);
mysql_select_db ($dn _name);

$result = mysql_query ("Select ' Name ' from ' The Users ' WHERE ' location ' = ' $location '");

while ($row = Mysql_fetch_array ($result, MYSQL_ASSOC))
{
echo $row [' name '];
}

Mysql_free_result ($result);

?>

At first glance there is no problem, but in fact there are some academic questions ...

This is not a way to Bind Column, as the SQL in the previous example says, $location place is susceptible to SQL injection. And then it's on display. Mysql_escape_string () (Note: 5.3.0 discard) and mysql_real_escape_string () to solve this problem, the whole of the narrative will become complex and ugly, and if the linked fields is much more, Can think of how the situation is ...


$query = sprintf ("SELECT * from Users WHERE user= '%s ' and password= '%s '",
Mysql_real_escape_string ($user),
Mysql_real_escape_string ($password));

mysql_query ($query);

?>

There was a lot of progress in php-mysqli, except through the Bind Column to solve the problem, and Transaction, Multi Query, and also provided the Object oriented style at the same time (the following php-mysq Examples of Li) and procedural style (above Php-mysql example) two writing methods ... Wait a minute.


$mysqli = new Mysqli ($db _host, $db _user, $db _password, $db _name);

$sql = "INSERT into ' users ' (ID, name, gender, location) VALUES (?,?,?,?)";
$stmt = $mysqli->prepare ($sql);

$stmt->bind_param (' dsss ', $source _id, $source _name, $source _gender, $source _location);

$stmt->execute ();

$stmt->bind_result ($id, $name, $gender, $location);

while ($stmt->fetch ())
{
Echo $id. $name. $gender. $location;
}

$stmt->close ();
$mysqli->close ();

?>

But there are some missing points here, such as Bind Result, which is a bit superfluous, but it's not really necessary, because the biggest problem is that it's not an abstract (abstraction) approach, so when the backend is changed to the repository, it's the beginning of the pain ...

So the PDO is out. (Note: Currently, Ubuntu and Debian say that PDO does not have a direct kit to install, but must be installed through PECL).

[Email protected]:~$ pecl Search PDO
=======================================
Package stable/(Latest) Local
PDO 1.0.3 (Stable) PHP Data Objects Interface.
PDO_4D 0.3 (Beta) PDO driver for 4d-sql database
Pdo_dblib 1.0 (Stable) Freetds/sybase/mssql driver for PDO
Pdo_firebird 0.2 (Beta) firebird/interbase 6 driver for PDO
PDO_IBM 1.3.2 (Stable) PDO driver for IBM databases
Pdo_informix 1.2.6 (Stable) PDO driver for IBM informix Informix databases
Pdo_mysql 1.0.2 (Stable) MYSQL driver for PDO
PDO_OCI 1.0 (Stable) Oracle call Interface driver for PDO
Pdo_odbc 1.0.1 (Stable) ODBC v3 Interface driver for PDO
Pdo_pgsql 1.0.2 (Stable) PostgreSQL driver for PDO
Pdo_sqlite 1.0.1 (Stable) SQLITE v3 Interface driver for PDO
Pdo_user 0.3.0 (Beta) userspace driver for PDO

Once the PECL is installed, you can manipulate the repository in the following ways:


$DSN = "mysql:host= $db _host;dbname= $db _name";
$DBH = new PDO ($DSN, $db _user, $db _password);
  • 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.