【问题标题】:PDO SQL Query returning fewer rows than expectedPDO SQL 查询返回的行数少于预期
【发布时间】:2017-09-22 18:45:27
【问题描述】:

我正在尝试构建一个 API 来从 mySQL 返回数据。我仍在尝试掌握 PDO。我有以下查询,该查询应基于单个参数从数据库返回所有行,但我注意到它通常返回的值比应返回的值少 1。有 2 行时返回 1,有 1 行时不返回任何内容。

我已附上数据库结果的屏幕截图。

任何建议将不胜感激。

[![<?php
// required header
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

// include database and object files
include_once '../config/database.php';
include_once '../objects/casualty.php';

// instantiate database and casualty object
$database = new Database();
$db = $database->getConnection();

// initialize object
$casualty = new casualty($db);

// set ID property of record to read
$casualty->id = isset($_GET\['id'\]) ? $_GET\['id'\] : die();

// query categorys
$stmt = $casualty->cemetery();
$num = $stmt->rowCount();

// check if more than 0 record found
if($num>0){

    // products array
    $casualtys_arr=array();
    $casualtys_arr\["records"\]=array();

    // retrieve our table contents
    // fetch() is faster than fetchAll()
    // http://stackoverflow.com/questions/2770630/pdofetchall-vs-pdofetch-in-a-loop
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
        // extract row
        // this will make $row\['name'\] to
        // just $name only
        extract($row);

        $casualty_item=array(
            "ID" => $ID,
            "Filename" => $fldgraphicname,  
            "Surname" => $fldsurname,  
            "FirstNameInitial" => $fldfirstnameinitial  

        );

        array_push($casualtys_arr\["records"\], $casualty_item);
    }

    echo json_encode($casualtys_arr);
}

else{
    echo json_encode(
        array("message" => "No Casualties found.")
    );
}
?>
<?php
class casualty{

    // database connection and table name
    private $conn;
    private $table_name = "tblcasualty";

    // object properties
    public $id;
    public $fldgraphicname;
        public $fldsurname;
        public $fldfirstnameinitial;

    public function __construct($db){
        $this->conn = $db;
    }

    public function cemetery(){            
        // query to read single record
                    $query = "SELECT *
                        FROM 
                            tblnames 
                        WHERE
                            tblcemetery_ID = ?
                            ORDER BY ID
                         ";
        $stmt = $this->conn->prepare( $query );

        // bind id of product to be updated
        $stmt->bindParam(1, $this->id, PDO::PARAM_INT);

        // execute query
        $stmt->execute();

        // get retrieved row
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        // set values to object properties
        $this->fldgraphicname = $row\['fldgraphicname'\];
        $this->fldsurname = $row\['fldsurname'\];
        $this->fldfirstnameinitial = $row\['fldfirstnameinitial'\];    

            return $stmt;
    }        

}
?>][1]][1]

【问题讨论】:

    标签: php pdo fetch


    【解决方案1】:

    在您的cemetery 函数中,您有一个fetch。这会将光标前进一个位置,因此当您返回主脚本并使用 fetch 时,您始终位于第二行。

    解决方案:

    1. 在函数中循环 fetch 并返回所有值的数组。

    1. 不要在函数中fetch 并返回结果集,以便您可以fetch 整个块。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多