Database connection: We analyze the PDO connection database through the following example.
The code is as follows |
Copy Code |
<?php $dbms = ' MySQL '; Database type Oracle with ODI, for developers, using a different database, just change this, do not have to remember so many functions $host = ' localhost '; Database host Name $dbName = ' Test '; The database used $user = ' root '; Database connection user Name $pass = '; The corresponding password $DSN = "$dbms: host= $host;d bname= $dbName"; // try { $DBH = new PDO ($DSN, $user, $pass); Initializes a PDO object, which is the creation of a database connection object $DBH echo "Connect successfully <br/>"; /* You can also do a search operation foreach ($dbh->query (' SELECT * from FOO ') as $row) { Print_r ($row); You can use Echo ($GLOBAL); To see these values } */ $DBH = null; catch (Pdoexception $e) { Die ("error!:"). $e->getmessage (). "<br/>"); } The default is not a long connection, and if you need a database long connection, you need to add a final parameter: Array (pdo::attr_persistent => true) to this: $db = new PDO ($DSN, $user, $pass, Array (pdo::attr_persistent => true)); ?> |
The result appears could not find driver error hint, then the reason for the search is not open php_pdo_mysql.dll template caused, know why the solution is simple.
Windows Download Modify PHP.ini
The code is as follows |
Copy Code |
Extension=php_pdo.dll Extension=php_pdo_mysql.dll Extension=php_pdo_pgsql.dll Extension=php_pdo_sqlite.dll Extension=php_pdo_mssql.dll Extension=php_pdo_odbc.dll Extension=php_pdo_firebird.dll ; Extension=php_pdo_oci8.dll |
A variety of PDO drivers, can be added to the full plus, but the back of the Php_pdo_oci8.dll, because I did not install the Oralce database, so without this, use a semicolon to annotate it, restart the Apache on it. Of course, if you use a different database, open the corresponding module on the OK.
If you are under Linux, be sure that the module pdo_mysql.so has been compiled into PHP.
Add under PHP.ini:
code is as follows |
copy code |
extension= pdo_mysql.so |