PHP uses pdostatement to process result sets

Source: Internet
Author: User

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

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.