<? 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 ...)