$db = new PDO($db_config['dsn'],$db_config['username'],$db_config['password']);//建立连接
$db->exec('SET NAMES '.C('DB_CHARSET'));
$sql = "select * from tab limit 1";
$sth = $db->prepare($sql);
$sth->execute();
$result = $sth->fetch();
print_r($result);//此处数据可以查询出来
$sql = "select * from tab limit 2";
$sth = $db->prepare($sql);
$sth->execute();//程序执行到此处就会报错
$sth->fetchAll();
dump($result);
报错信息:Fatal error: Call to a member function execute() on a non-object in

解决方案 »

  1.   

    php 与pdo 数据库连接实例
    http://www.111cn.net/phper/21/pdo_php.htm
      

  2.   

    官方示例:
    <?php
    /* Execute a prepared statement by passing an array of values */
    $sql = 'SELECT name, colour, calories
        FROM fruit
        WHERE calories < :calories AND colour = :colour';
    $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    $sth->execute(array(':calories' => 150, ':colour' => 'red'));
    $red = $sth->fetchAll();
    $sth->execute(array('calories' => 175, 'colour' => 'yellow'));
    $yellow = $sth->fetchAll();
    ?>
      

  3.   

    由此可见,prepare主要用来执行绑定参数的sql语句,你不能分别弄一个sql语句,分别弄一个prepare