Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用PDO fetch查询Mysql返回false_Php_Mysql_Pdo - Fatal编程技术网

Php 使用PDO fetch查询Mysql返回false

Php 使用PDO fetch查询Mysql返回false,php,mysql,pdo,Php,Mysql,Pdo,我试图在PDO中查询我的数据库,并将结果输出到表单上。但是,fetch语句不起作用。代码是 try { include '../../config/database.php'; $database = new Database(); $db = $database->getConnection(); //prepare query $query = "select payment_id, payment_supplier, payment_ref, payme

我试图在PDO中查询我的数据库,并将结果输出到表单上。但是,fetch语句不起作用。代码是

try {
include '../../config/database.php';
$database = new Database();
$db = $database->getConnection();


//prepare query
$query = "select 
            payment_id, payment_supplier, payment_ref, payment_cost_rating, payment_amount 
        from 
            payments 
        where 
            payment_id = ? 
        limit 0,1";

$stmt = $db->prepare( $query );

//this is the first question mark
$stmt->bindParam(1, $_REQUEST['myData']);

//execute our query
if($stmt->execute()){
var_dump($stmt->fetch());
    //store retrieved row to a variable
    $row = $stmt->fetch(PDO::FETCH_ASSOC); 

    //values to fill up our form
    $payment_id = $row['payment_id'];
    $payment_supplier = $row['payment_supplier'];
    $payment_ref = $row['payment_ref'];
    $payment_cost_rating = $row['payment_cost_rating'];
    $payment_amount = $row['payment_amount'];

}else{
    echo "Unable to read record.";
}
}

objectPDOStatement3 1{[queryString]=>string157选择付款id、付款供应商、付款参考、付款成本评级、付款金额,其中付款id=?限制0,1}

但是fetch总是返回false。这是附带的database.php文件(如果有帮助)

class Database{

// database credentials
private $host = "localhost";
private $db_name = "test-project";
private $username = "root";
private $password = "";
public $conn;

// get the database connection
public function getConnection(){

    $this->conn = null;

    try{
        $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
    }catch(PDOException $exception){
        echo "Connection error: " . $exception->getMessage();
    }

    return $this->conn;
}}

我在这里遗漏了什么?

添加对执行结果的检查,并查看错误:

if($stmt->execute()){
    ...
}else{
    echo "Unable to read record:". print_r($stmt->errorInfo(), true);
}

问题在于fetch被使用了两次

可以这样做:


这应该可以很好地工作。

永远不要将“else”语句称为$stmt->execute;作品然而,调用fetch不起作用。如果只使用一条记录限制结果,那么您将在var_dump中看到返回的记录。如果查询返回空结果,则fetch或no records。在var_dump..fetch always之后,fetch的下一个调用将失败。如果fetch总是false,我建议您在任何sql客户机程序中检查查询及其结果。
if($stmt->execute()){
    ...
}else{
    echo "Unable to read record:". print_r($stmt->errorInfo(), true);
}
if($stmt->execute()){
    //store retrieved row to a variable
    $row = $stmt->fetch(PDO::FETCH_ASSOC);

     // display the row after fetching the contents.
    var_dump($row); 

    //values to fill up our form
    $payment_id = $row['payment_id'];
    $payment_supplier = $row['payment_supplier'];
    $payment_ref = $row['payment_ref'];
    $payment_cost_rating = $row['payment_cost_rating'];
    $payment_amount = $row['payment_amount'];

}else{
    echo "Unable to read record.";
}