Php connection to mysql ~ Master skipped

Source: Internet
Author: User
Php connection to mysql ~ Master skipped the recent installation of a new environment in linux, php5.6 + mysql5.5 + nginx. Then, an error occurs when you use the original mysql connection to the database.

The reason is that the database connection method is too old. We recommend that you use mysqli and PDO to connect to the database.

Well, we can't lag behind. using mysqli is actually much simpler, but PDO seems simpler. Efficiency will also be improved. According to the official documentation, it seems that some risks of SQL injection are also blocked. So my blog today is about how to use PDO to connect to mysql in php!


[What is PDO]

PDO is a major new feature of PHP 5, because php4/php3 before PHP 5 was a bunch of database extensions to connect to and process various databases, php_mysql.dll, php_pgsql.dll, php_mssql.dll, php_sqlite.dll, and other extensions to connect to MySQL, PostgreSQL, ms SQL Server, and SQLite. Similarly, we must use ADOdb, PEAR: DB, PHPlib :: database abstract classes such as DB are very cumbersome and inefficient to help us. after all, how can we directly use C/C ++ to write php code with high efficiency? Therefore, the emergence of PDO is inevitable. you should accept it with a calm learning attitude. maybe you will find it can reduce your efforts.



The following describes the php-based version of PDO:

PDO is released in PHP 5.1. that is to say, the version earlier than 5.1 does not support PDO. It can also be used in the PECL extension of PHP5.0.


How to use PDO:

Here we will take the PHP Gold partner mysql as an example:

PDO_MYSQL: PDO_MYSQL is the driver that can connect to the mysql database through the PDO interface (note: it is only used in mysql 3.x or later versions ).

Install: open php. in the INI file, you can find the following code. here, we can see that the mysql driver has been opened by default (there is no semicolon before for comment). If you need to connect to other databases, add drivers for other databases on your own (remove the semicolon before the corresponding item and add the driver without it ).

// The PDO driver extension = php_pdo.dll extension = php_pdo_firebird.dll for each database // Firebird extension = php_pdo_informix.dll // Informix extension = Login // SQL server extension = Login // mysql extension = development/release // oracle extension = php_pdo_oci8.dll extension = php_pdo_odbc.dll // DB2 extension = php_pdo_pgsql.dll // PostgreSQL extension = php_pdo_sqlite.dll // SQLite

Connection: create a connection by creating an instance of the PDO base class.

// Connect to the database $ db = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass );

// Handle connection error try {$ db = new PDO ('MySQL: host = localhost; dbname = test', $ user, $ pass ); // query foreach ($ db-> query ('select * from Foo') as $ row) {print_r ($ row) ;}$ dbh = null ;} catch (PDOException $ e) {print "Error! : ". $ E-> getMessage ()."
"; Die ();}


// Close the connection $ db = null;

Connect to the mysql instance in the PDO mode:

 _ ToString ()."
") ;}} Publicfinalfunctionquery ($ SQL) {try {returnparent: query ($ this-> setString ($ SQL);} catch (PDOException $ e) {die ("Error :". $ e->__ toString ()."
") ;}} PrivatefinalfunctionsetString ($ SQL) {echo" query processing "; return $ SQL ;}$ db = new db (); $ db-> setAttribute (PDO :: ATTR_CASE, PDO: CASE_UPPER); foreach ($ db-> query ('select * from table_name ') as $ row) {print_r ($ row );} $ db-> exec ('delete from table_name where id = 11');?>


More PDO methods:

PDO: beginTransaction? Start a transaction PDO: commit? Commit a transaction PDO ::__ construct? Create a PDO instance PDO: errorCode? Obtain sqlstate pdo: errorInfo? related to the last operation of the database handle? Fetch extended error information associated with the last operation on the database handle PDO: exec? Execute an SQL statement and return the affected number of lines PDO: getAttribute? Retrieve the property PDO: getAvailableDrivers of a database connection? Returns an array of available drivers, PDO: inTransaction? Check whether PDO: lastInsertId is in a transaction? Returns the ID or sequence value of the last inserted row. PDO: prepare? Prepares a statement for execution and returns a statement object PDO: query? Executes an SQL statement, returning a result set as a PDOStatement object PDO: quote? Quotes a string for use in a query. PDO: rollBack? Roll back a transaction PDO: setAttribute? Set attributes

PDOStatement: bindColumn? Bind a column to a PHP variable PDOStatement: bindParam? Bind a parameter to the specified variable name PDOStatement: bindValue? Bind a value to a parameter PDOStatement: closeCursor? Close the cursor so that the statement can be executed again. PDOStatement: columnCount? The number of columns in the returned result set PDOStatement: debugDumpParams? Print an SQL preprocessing command PDOStatement: errorCode? Obtain SQLSTATE PDOStatement related to the last statement handle operation: errorInfo? Obtain the extension error message PDOStatement: execute? related to the last statement handle operation? Execute a preprocessing statement PDOStatement: fetch? Obtain the next PDOStatement: fetchAll? row from the result set? Returns an array of all rows in the result set, PDOStatement: fetchColumn? Returns a separate column from the next row in the result set. PDOStatement: fetchObject? Obtain the next row and return it as an object. PDOStatement: getAttribute? Retrieve a statement attribute PDOStatement: getColumnMeta? Returns the metadata PDOStatement: nextRowset? of a column in the result set? Push the statement handle of a multi-row set to the next row set PDOStatement: rowCount? Returns the number of rows affected by the previous SQL statement PDOStatement: setAttribute? Set a statement attribute PDOStatement: setFetchMode? Set the default query mode for the statement.

Exception: getMessage? Obtains the content of an exception message. Exception: getPrevious? Returns the first Exception in the Exception chain: getCode? Get Exception code Exception: getFile? Get the program file name that encountered an Exception: getLine? Obtain the row number of the code with an Exception in the file. Exception: getTrace? Get Exception tracing information Exception: getTraceAsString? Obtain the Exception tracing information of the string type. Exception: toString? Convert an Exception object to a string Exception: clone? Clone exception

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.