PDO links to mysql Study Notes

Source: Internet
Author: User

<? Php // PDO link to mysql
// Dsn: // dsn01 $ dsn = 'mysql: host = localhost; dbname = mysql'; // $ dsn = 'mysql: host = localhost; dbname = mysql [; port = 3306; charset = UTF8] ';/** dsn02 $ dsn = 'uri: file: // C: \ xampp \ htdocs \ pdo \ config.txt'character config.txt: mysql: host = localhost; dbname = mysqllinux and windows are feasible $ dsn = 'uri: file: // home/pdo/config.txt '; * // ** dsn03 $ dsn = 'mydb'; php. add: pdo. dsn. mydb = 'mysql: host = localhost; dbname = mysql' */$ user = 'Root'; $ pwd = ''; // if the attempt to connect to the requested database fails, PDO ::__ construct () throws a PDO exception (PDOException ). Try {$ link = new PDO ($ dsn, $ user, $ pwd);} catch (PDOException $ e) {echo 'Connection failed :'. $ e-> getMessage () ;}$ userSet = $ link-> query ('select * from user'); print_r ($ userSet );

 

<? PHP // transaction // start a transaction and close automatic commit $ dbh-> beginTransaction ();/* do something */if (true) {/* submit changes */$ dbh-> commit ();} else {/* identify errors and roll back changes */$ dbh-> rollBack ();} /* The database connection is now returned to the automatic submission mode * // ** note that some databases, including MySQL, have DDL statements such as deleting or creating data tables in a transaction, it automatically leads to an implicit commit. Implicit commit will not roll back any changes within this transaction range. For more information, see http://dev.mysql.com/doc/refman/5.0/en/implicit-commit.html */

 

<? Php/** preprocessing statement PDOStatement class PDO class returns the PDOStatement instance method public PDOStatement prepare (string $ statement [, array $ driver_options = array ()]) public PDOStatement query (string $ statement) * // What is the difference between bindParam and bindValue? /* Use the INOUT parameter to call a stored procedure and use the PDO: PARAM _ * constant to explicitly specify the parameter type. To return an INOUT parameter from a stored procedure, set the PDO: PARAM_INPUT_OUTPUT bit for the data_type parameter using the bitwise OR operator. */$ Color = 'red'; $ something = $ dbh-> prepare ('call puree_fruit (?) '); $……-> BindParam (1, $ color, PDO: PARAM_STR | PDO: PARAM_INPUT_OUTPUT, 12); $……> execute (); print ("After pureeing fruit, the color is: $ color ");

 

(To be continued ...)

Related Article

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.