php pdo test | LSABLOG

首页 » Program » PHP&&Mysql » 正文

php pdo test

I did a php pdo test,firstly I created a database named pdotest,then I created a table named books and inserted some records for test.Finally I used pdo to get thoes records.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body,td,th {
    font-size: 18px;

<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">

        <table width="400" border="0" cellpadding="0" cellspacing="0">
            <td height="30" align="center"><strong>id</strong></td>
            <td align="center"><strong>bookname</strong></td>
            <td align="center"><strong>bookprice</strong></td>
            <td align="center"><strong>edit</strong></td>
try {
    $pdo = new PDO($dsn, $user, $pass);     
    $query='select * from books';   
    echo 'errorcode:'.$pdo->errorCode();
    echo '<br/>';
            <td height="22" align="center" valign="middle"><?php echo $res['id'];?></td>
            <td align="center" valign="middle"><?php echo $res['bookname'];?></td>
            <td align="center" valign="middle"><?php echo $res['bookprice'];?></td>
            <td align="center" valign="middle"><a href="#">delete</a></td>
          } catch (PDOException $e) {
    die ("Error!: " . $e->getMessage() . "<br/>");

If you want to see the error code when the sql is error,you should use $pdo->query($query) instead of prepare and execute,I don’t know why.