<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->beginTransaction ()); // bool(true)
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ dbh ->beginTransaction ();
$ dbh ->exec ("UPDATE users SET name = 'Jerry' WHERE id = 1 " );
var_dump ($ dbh ->commit ()); // bool(true)
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->errorCode ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->errorInfo ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->exec ("UPDATE users SET name = 'Jerry' WHERE id = 1 " )); // int(1)
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->getAttribute (PDO ::ATTR_AUTOCOMMIT ));
var_dump ($ dbh ->getAttribute (PDO ::ATTR_CASE ));
var_dump ($ dbh ->getAttribute (PDO ::ATTR_CLIENT_VERSION ));
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump (PDO ::getAvailableDrivers ());
var_dump ($ dbh ->getAvailableDrivers ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->inTransaction ()); // bool(false)
$ dbh ->beginTransaction ();
var_dump ($ dbh ->inTransaction ()); // bool(true)
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->lastInsertId ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->prepare ("SELECT * FROM users WHERE id = :id " )); // object(PDOStatement)#2 (1) { ["queryString"]=> string(34) "SELECT * FROM users WHERE id = :id" }
var_dump ($ dbh ->prepare ("SELECT * FROM users WHERE id = ? " )); // object(PDOStatement)#2 (1) { ["queryString"]=> string(32) "SELECT * FROM users WHERE id = ?" }
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->query ("SELECT * FROM users " )); // object(PDOStatement)#2 (1) { ["queryString"]=> string(19) "SELECT * FROM users" }
foreach ($ dbh ->query ("SELECT * FROM users " ) as $ row ) {
var_dump ($ row ['id ' ], $ row ['name ' ]);
}
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ string = 'foo ' ;
var_dump ($ dbh ->quote ($ string )); // string(5) "'foo'"
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ dbh ->beginTransaction ();
$ dbh ->exec ("UPDATE users SET name = 'Jerry' WHERE id = 1 " );
var_dump ($ dbh ->rollBack ()); // bool(true)
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
var_dump ($ dbh ->setAttribute (PDO ::ATTR_CASE , PDO ::CASE_LOWER )); // bool(true)
var_dump ($ dbh ->setAttribute (PDO ::ATTR_CASE , PDO ::CASE_UPPER )); // bool(true)
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->bindColumn ('id ' , $ id )); // bool(true)
var_dump ($ sth ->bindColumn ('name ' , $ name )); // bool(true)
while ($ sth ->fetch ()) {
var_dump (sprintf ('#%d %s ' , $ id , $ name ));
}
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = :id AND name = :name " );
$ id = 1 ;
$ name = 'Tom ' ;
var_dump ($ sth ->bindParam (':id ' , $ id , PDO ::PARAM_INT )); // bool(true)
var_dump ($ sth ->bindParam (':name ' , $ name , PDO ::PARAM_STR )); // bool(true)
$ sth ->execute ();
var_dump ($ sth ->fetchAll ());
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = ? AND name = ? " );
$ id = 1 ;
$ name = 'Tom ' ;
var_dump ($ sth ->bindParam (1 , $ id , PDO ::PARAM_INT )); // bool(true)
var_dump ($ sth ->bindParam (2 , $ name , PDO ::PARAM_STR )); // bool(true)
$ sth ->execute ();
var_dump ($ sth ->fetchAll ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = :id AND name = :name " );
var_dump ($ sth ->bindValue (':id ' , 1 , PDO ::PARAM_INT )); // bool(true)
var_dump ($ sth ->bindValue (':name ' , 'Tom ' , PDO ::PARAM_STR )); // bool(true)
$ sth ->execute ();
var_dump ($ sth ->fetchAll ());
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = ? AND name = ? " );
var_dump ($ sth ->bindValue (1 , 1 , PDO ::PARAM_INT )); // bool(true)
var_dump ($ sth ->bindValue (2 , 'Tom ' , PDO ::PARAM_STR )); // bool(true)
$ sth ->execute ();
var_dump ($ sth ->fetchAll ());
PDOStatement::closeCursor
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
$ sth ->fetch ();
var_dump ($ sth ->closeCursor ()); // bool(true)
PDOStatement::columnCount
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->columnCount ());
PDOStatement::debugDumpParams
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = :id AND name = :name " );
$ sth ->bindValue (':id ' , 1 , PDO ::PARAM_INT );
$ sth ->bindValue (':name ' , 'Tom ' , PDO ::PARAM_STR );
$ sth ->execute ();
var_dump ($ sth ->debugDumpParams ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->errorCode ()); // string(5) "00000"
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->errorInfo ()); // array(3) { [0]=> string(5) "00000" [1]=> NULL [2]=> NULL }
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = :id AND name = :name " );
$ sth ->bindValue (':id ' , 1 , PDO ::PARAM_INT );
$ sth ->bindValue (':name ' , 'Tom ' , PDO ::PARAM_STR );
var_dump ($ sth ->execute ()); // bool(true)
var_dump ($ sth ->fetchAll ());
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = :id AND name = :name " );
var_dump ($ sth ->execute ([':id ' => 1 , ':name ' => 'Tom ' ])); // bool(true)
var_dump ($ sth ->fetchAll ());
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = ? AND name = ? " );
$ sth ->bindValue (1 , 1 , PDO ::PARAM_INT );
$ sth ->bindValue (2 , 'Tom ' , PDO ::PARAM_STR );
var_dump ($ sth ->execute ()); // bool(true)
var_dump ($ sth ->fetchAll ());
$ sth = $ dbh ->prepare ("SELECT * FROM users WHERE id = ? AND name = ? " );
var_dump ($ sth ->execute ([1 , 'Tom ' ])); // bool(true)
var_dump ($ sth ->fetchAll ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->fetch ());
$ sth ->execute ();
var_dump ($ sth ->fetch (PDO ::FETCH_ASSOC ));
$ sth ->execute ();
var_dump ($ sth ->fetch (PDO ::FETCH_OBJ ));
<?php
class Foo
{
//
}
/**
* Return formatted string.
*
* @param int $id
* @param string $name
* @return string
*/
function foo (int $ id , string $ name ): string
{
return sprintf ('#%d %s ' , $ id , $ name );
}
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->fetchAll ());
$ sth ->execute ();
var_dump ($ sth ->fetchAll (PDO ::FETCH_ASSOC ));
$ sth ->execute ();
var_dump ($ sth ->fetchAll (PDO ::FETCH_COLUMN , 1 ));
$ sth ->execute ();
var_dump ($ sth ->fetchAll (PDO ::FETCH_COLUMN | PDO ::FETCH_GROUP ));
$ sth ->execute ();
var_dump ($ sth ->fetchAll (PDO ::FETCH_CLASS , 'Foo ' ));
$ sth ->execute ();
var_dump ($ sth ->fetchAll (PDO ::FETCH_FUNC , 'foo ' ));
PDOStatement::fetchColumn
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->fetchColumn ());
var_dump ($ sth ->fetchColumn (1 ));
PDOStatement::fetchObject
<?php
class Foo
{
//
}
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->fetchObject ());
var_dump ($ sth ->fetchObject ('Foo ' ));
PDOStatement::getColumnMeta
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->getColumnMeta (0 ));
var_dump ($ sth ->getColumnMeta (1 ));
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->fetch ());
var_dump ($ sth ->nextRowset ());
var_dump ($ sth ->fetch ());
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("UPDATE users SET name = :name WHERE id = :id " );
$ sth ->bindValue (':name ' , 'Jerry ' , PDO ::PARAM_STR );
$ sth ->bindValue (':id ' , 1 , PDO ::PARAM_INT );
$ sth ->execute ();
var_dump ($ sth ->rowCount ()); // int(1)
PDOStatement::setFetchMode
<?php
try {
$ dbh = new PDO ('mysql:host=127.0.0.1;port=3306;dbname=test ' , 'root ' , 'root ' );
} catch (PDOException $ e ) {
echo $ e ->getMessage ();
}
$ sth = $ dbh ->prepare ("SELECT * FROM users " );
$ sth ->execute ();
var_dump ($ sth ->setFetchMode (PDO ::FETCH_ASSOC )); // bool(true)
var_dump ($ sth ->fetchAll ());