1.PDOStatement
In PDO processing, a Pdostatement object is returned whenever a result set is returned or a preprocessing is used
2. Processing the result set use Fetch () to get a record in the result set
1<?PHP2 $dsn= "Mysql:host=127.0.0.1;dbname=example";3 $user= "Root";4 $password= ' ';5 Try6 {7 $pdo=NewPDO ($dsn,$user,$password);8 $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);9 Ten $pdostatement=$pdo->query (' SELECT * from student '); One A while($row=$pdostatement-fetch ()) - { - Print_r($row);Echo"<br>"; the } - - } - Catch(pdoexception$e) + { - Echo $e-getMessage (); + } A at $pdo=NULL; -?>
You can pass parameters to specify the return form of the result set
$row=$pdostatement->fetch (PDO::FETCH_ASSOC)// returned as an associative array
$row=$pdostatement->fetch (pdo::fetch_num)// returned as an indexed array
Use Fetchall () to turn the result set directly into a two-dimensional array to return
1<?PHP2 $dsn= "Mysql:host=127.0.0.1;dbname=example";3 $user= "Root";4 $password= ' ';5 Try6 {7 $pdo=NewPDO ($dsn,$user,$password);8 $pdo->setattribute (pdo::attr_errmode,pdo::errmode_exception);9 Ten $pdostatement=$pdo->query (' SELECT * from student '); One A Print_r($pdostatement-Fetchall ()); - - } the Catch(pdoexception$e) - { - Echo $e-getMessage (); - } + - $pdo=NULL; +?>3.rowCount () returns the number of result set rows or the number of rows affected
$pdostatement=$pdo->query (' SELECT * from student '); Echo $pdostatement->rowcount (); // directly returns the number of rows queried
$pdostatement=$pdo->prepare ("DELETE from Student"); $pdostatement, execute (); Echo $pdostatement->rowcount (); // show how many rows were deleted
PHP uses pdostatement to process result sets