这种转换为PDO是否正确?

时间:2016-07-20 17:50:51

标签: php mysql pdo

我正在尝试将以下代码转换为PDO:

    $result = DB::query('SELECT * FROM webchat_lines WHERE id > '.$lastID.' ORDER BY id ASC');

    $chats = array();
    while($chat = $result->fetch_object()){

        // Returning the GMT (UTC) time of the chat creation:

        $chat->time = array(
            'hours'        => gmdate('H',strtotime($chat->ts)),
            'minutes'    => gmdate('i',strtotime($chat->ts))
        );

        $chat->gravatar = Chat::gravatarFromHash($chat->gravatar);

        $chats[] = $chat;
    }

    return array('chats' => $chats);

我的失败尝试:

    $paramArray = array(
         ":lastID" => $lastID
    );
    $result = DB::query('SELECT * FROM webchat_lines WHERE id > :lastID ORDER BY id ASC', $paramArray);

    $chats = array();
    $chats = array();
    while($chat = $result->fetch(PDO::FETCH_OBJ)){ //CHANGED THIS PART

        // Returning the GMT (UTC) time of the chat creation:

        $chat->time = array(
            'hours'     => gmdate('H',strtotime($chat->ts)),
            'minutes'   => gmdate('i',strtotime($chat->ts))
        );

        $chat->gravatar = Chat::gravatarFromHash($chat->gravatar);

        $chats[] = $chat;
    }

    return array('chats' => $chats);

编辑:

在尝试接收从前一个php代码返回的信息时,正在进行ajax请求。可以在此处找到非PDO代码的原始来源: http://tutorialzine.com/2010/10/ajax-web-chat-css-jquery/

问题不应该$chat = $result->fetch(PDO::FETCH_OBJ)等同于$chat = $result->fetch_object()?我能做错什么?

编辑2:

我在error_log中发现了以下错误:

  

PHP致命错误:调用成员函数   fetch()在非对象中   在第151行

似乎$result不被视为对象。定义$result的代码已放置在两个示例中。函数DB :: query()的代码如下:

public static function query($q, $paramArray=array()){

    $stmt = self::$instance->conn->prepare($q);
    foreach ($paramArray as $key => $value)
    {
        $stmt->bindParam($key, $value);
    }
    return $stmt->execute();

    //the older version of this function had this code:
         //return self::$instance->conn->query($q); 
    //but I replaced it with the previous so that I can bind the
    //parameters and prepare the sql statement
}

不应该$result因此成为一个对象?它正在返回$stmt->execute();

1 个答案:

答案 0 :(得分:2)

通常不可能将PDO用作旧版mysql_*()代码的直接替代品,此处需要进行一些概念性重组。

PDOStatement::execute() never returns an object任何类型。相反,它的返回值是基于SQL语句成功或失败的布尔值TRUE/FALSE

在之前的代码中,您从query()方法返回结果资源,稍后从中获取,在fetch循环内执行其他逻辑。由于execute()不会返回可以这种方式使用的对象,我建议改为在query()方法中执行完整的提取操作,然后再使用简单的foreach循环来制作使用行集。也可以从return $stmt;方法query(),但这似乎在逻辑上混乱。

public static function query($q, $paramArray=array()){

    $stmt = self::$instance->conn->prepare($q);
    foreach ($paramArray as $key => $value)
    {
        $stmt->bindParam($key, $value);
    }
    // Execute the statement
    $stmt->execute();
    // Then fetch and return all rows as an array of objects
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}

然后,要使用返回的行集,请为while循环切换foreach fetch循环:

// $result is now an array of objects...
$result = DB::query('SELECT * FROM webchat_lines WHERE id > :lastID ORDER BY id ASC', $paramArray);

$chats = array();
$chats = array();
foreach ($result as $chat){
    // Returning the GMT (UTC) time of the chat creation:
    $chat->time = array(
        'hours'     => gmdate('H',strtotime($chat->ts)),
        'minutes'   => gmdate('i',strtotime($chat->ts))
    );

    $chat->gravatar = Chat::gravatarFromHash($chat->gravatar);

    $chats[] = $chat;
}
return array('chats' => $chats);
// etc...

从上下文中的代码中,很难判断您是否已将PDO配置为在失败时抛出异常。我建议这样做,因为默认情况下,当execute()或其他方法失败时,它将无提示错误。在创建PDO实例的位置,设置适当的属性:

// Turn on PDO exceptions when the instance is first created
// That may not have been within this class, but difficult to 
// tell from the limited code posted.
self::$instance->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);