使用简单的SQL查询无法找到准确的记录。 (PHP MySQLi)

时间:2018-05-30 15:44:10

标签: php sql mysqli

我在公司的新计划中找到特定记录时遇到问题。这是一个非常简单的SQL查询,我只能在我的生命中通过。

有人能看到问题吗?

public function loadByKey($partNumber, $customerNumber, $rev){
    $query = 'SELECT *'
            .' FROM '.$this->myTableName
            .' WHERE PartNumber = ?'
            .' AND CustomerNumber = ?'
            .' AND Rev = ?';

    $conn = (new DBConnector)->connect();

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $statement = $conn->prepare($query);
    //var_dump($statement);
    //var_dump($query);
    //var_dump($conn);
    $statement->bind_param('sis', $partNumber, $customerNumber, $rev);
    $result = $statement->execute();
    //var_dump($result);
    //var_dump($statement);
    //var_dump($customerNumber);
    //var_dump($rev);

    if ($statement->num_rows == 1) {
    // output data of each row
        $this->Properties = $statement->fetch_assoc();
        var_dump($this->Properties);
        $conn->close();
        return true;
    } else {
        $this->Properties = [];
        $conn->close();
        return false;
    }  

}

1 个答案:

答案 0 :(得分:1)

缺少mysqli_stmt_store_result? - ficuscr 在深入研究并阅读我的旧代码之后,这就是答案:

public function loadByKey($partNumber, $customerNumber, $rev){
    $query = 'SELECT *'
            .' FROM '.$this->myTableName
            .' WHERE PartNumber = ?'
            .' AND CustomerNumber = ?'
            .' AND Rev = ?';

    $conn = (new DBConnector)->connect();

    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 

    $statement = $conn->prepare($query);
    //var_dump($statement);
    //var_dump($query);
    //var_dump($conn);
    $statement->bind_param('sis', $partNumber, $customerNumber, $rev);
    $statement->execute();
    $result = $statement->get_result();
    var_dump($result);
    //var_dump($statement);
    //var_dump($customerNumber);
    //var_dump($rev);

    if ($result->num_rows == 1) {
    // output data of each row
        $this->Properties = $result->fetch_assoc();
        var_dump($this->Properties);
        $conn->close();
        return true;
    } else {
        $this->Properties = [];
        $conn->close();
        return false;
    }  

}