Let's take a look at how php connects to the database. what is the solution?-php Tutorial

Source: Internet
Author: User
Tags informix
How does php connect to the database? Www.44lanweb. in comlan_serverphpphp_mysql_connect.aspx, mysql_connect () is used for connection. I remember the teacher said that it is best to use PDO or do you still have to connect to ADO or something, what connection does PHP use to connect to the database? ------ Solution ---- Let's look at how php connects to the database?
Http://www.44lanweb.com/lan_server/php/php_mysql_connect.aspx
Mysql_connect () is used for connection. I remember the teacher said that it is best to use PDO or do you still need ADO connection or something. I don't know clearly, what connection does PHP use to connect to the database?

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

Pdo seems to have shielded the differences between databases and provided unified interfaces.

------ Solution --------------------
The difference between databases. generally, php works with mysql. Therefore, mysql_connect is widely used. However, if the database is access or mssql, you must consider using links such as ADO or oledb.
The above is my personal understanding and is for reference only ~~
------ Solution --------------------
The best teacher for this problem is Uncle google, who needs to add additional pdo extension support. However, it seems that I have read the new version and will directly support it... or I remember it wrong, because I didn't use PDO, so others need to add more.

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

PHP-MySQL is the original Extension for PHP to operate MySQL data sources. I of PHP-MySQLi stands for Improvement and provides the opposite Extension function, security is also increased. In contrast, PDO (PHP Data Object) provides an action Layer to operate on Data objects. In fact, it cannot be seen that there is any difference between them, so let's look at the program directly...

First, let's take a look at a program developed using PHP-MySQL. such examples are often used around the world:


Mysql_connect ($ db_host, $ db_user, $ db_password );
Mysql_select_db ($ dn_name );

$ Result = mysql_query ("SELECT 'name' FROM '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 questions after learning...

This method cannot be Bind Column. in the preceding SQL statement, $ location is prone to SQL Injection. In the end, mysql_escape_string () (used later than 5.3.0) and mysql_real_escape_string () are displayed to solve this problem, the whole story will become ugly and ugly, and if there are too many places, you can see how it will happen...


$ Query = sprintf ("SELECT * FROM users WHERE user = '% s' AND password =' % s '",
Mysql_real_escape_string ($ user ),
Mysql_real_escape_string ($ password ));

Mysql_query ($ query );

?>

In PHP-MySQLi, there are a lot of steps in progress. in addition to resolving the above problems through the Bind Column, it also supports Transaction, Multi Query, and also provides the Object oriented style (method of the following PHP-MySQLi samples) and Procedural style (method of the above PHP-MySQL samples) two methods... And so on.


$ 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 ('dss', $ 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 ();

?>

However, we can see that there are still some shortcomings in this case. for example, if Bind Result is obtained, there will be more than one problem, but it is irrelevant in this case, because the biggest problem is that this is not an abstract action method, it is the beginning of pain when more information is provided at the end...

Now, PDO has appeared. (currently, Ubuntu and Debian do not have a direct suite to install, but must pass 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

After PECL security measures are passed, you can operate the resource 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.