PHP可以解释这个var转储

时间:2015-01-06 13:28:41

标签: php mysql pdo

找到结果时返回此var转储,如果找到结果,为什么会抛出错误。

object(DB)#3 (5) { ["_pdo":"DB":private]=> object(PDO)#4 (0) { } ["_query":"DB":private]=> object(PDOStatement)#6 (1) { ["queryString"]=> string(80) "SELECT * FROM `activity` WHERE `name` = ? AND `act_date` = ? AND `time_from` = ?" } ["_error":"DB":private]=> bool(false) ["_results":"DB":private]=> array(3) { [0]=> object(stdClass)#7 (7) { ["activity_id"]=> string(1) "6" ["name"]=> string(7) "Archery" ["act_date"]=> string(10) "12/01/2015" ["time_from"]=> string(5) "10.00" ["time_to"]=> string(5) "11.00" ["num_people"]=> string(2) "12" ["booking_id"]=> string(1) "1" } [1]=> object(stdClass)#8 (7) { ["activity_id"]=> string(2) "13" ["name"]=> string(7) "Archery" ["act_date"]=> string(10) "12/01/2015" ["time_from"]=> string(5) "10.00" ["time_to"]=> string(5) "11.00" ["num_people"]=> string(2) "10" ["booking_id"]=> string(1) "1" } [2]=> object(stdClass)#9 (7) { ["activity_id"]=> string(2) "56" ["name"]=> string(7) "Archery" ["act_date"]=> string(10) "12/01/2015" ["time_from"]=> string(5) "10.00" ["time_to"]=> string(5) "11.00" ["num_people"]=> string(2) "10" ["booking_id"]=> string(1) "2" } } ["_count":"DB":private]=> int(3) }

当查询找不到任何结果时,将返回此var转储:

object(DB)#3 (5) { ["_pdo":"DB":private]=> object(PDO)#4 (0) { } ["_query":"DB":private]=> object(PDOStatement)#6 (1) { ["queryString"]=> string(80) "SELECT * FROM `activity` WHERE `name` = ? AND `act_date` = ? AND `time_from` = ?" } ["_error":"DB":private]=> bool(false) ["_results":"DB":private]=> array(0) { } ["_count":"DB":private]=> int(0) } 

调用查询:

!$activity->checkDateTimeAvailability($act_name, $act_date, $time_from)

// Method to check if the activity is available at the requested date and time.
public function checkDateTimeAvailability($act_name, $act_date, $time_from) {
    $fields = array($act_name, $act_date, $time_from);
    $result = $this->_db->query("SELECT * FROM `activity` WHERE `name` = ? AND `act_date` = ?
    AND `time_from` = ?", $fields);

    var_dump($result);
    if(!empty($result)){
        echo "query successful";
        return true;
    } else {
        return false;
    }
}   

数据库查询方法:

// Generic query method.
public function query($sql, $params = array()) {
    // reset to ensure an error from a previous query is not returned. 
    $this->_error = false;

    if($this->_query = $this->_pdo->prepare($sql)) {
        $x = 1;
        if(count($params)) {
            foreach($params as $param) {
                $this->_query->bindValue($x, $param);
                $x++;
            }
        }

        if($this->_query->execute()) {
            $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
            $this->_count = $this->_query->rowCount();

        } else {
            $this->_error = true;
            echo 'Error: ', $this->_error;
        }
    }

    return $this;
}

1 个答案:

答案 0 :(得分:0)

看起来这段代码非常合适。

首先,您将DB对象的链接保存到$result变量,然后使用var_dump,并且应该有一条消息“查询成功”,checkDateTimeAvailability函数将始终将true返回给主叫方。

您可能应该从DB对象或所选行计数中获取查询结果。 !empty($result)将始终为真,因为$result - 是从查询中获取的对象,而不是数据数组。

EDITED

你有几个机会: 第一 查找DB类,其中描述了query方法,并查找Count()GetResult()等方法。如果存在这样的方法 - 使用它们。 if ($result->count() > 0){ echo "query successful"; return true; } else { return false; } 第二 如果DB类中没有这样的方法,并且这不是某些库类,则可以添加自己的Count方法实现

public function count() { return $this->_count; } 如果我能看到所有的DB类,那就容易多了。