PDO SQL 查询返回的行数少于预期
PDO SQL Query returning fewer rows than expected
我正在尝试从 mySQL 构建 API 到 return 的数据。我仍在努力掌握 PDO 的窍门。我有下面的查询,它应该 return 来自数据库的所有行,基于单个参数,但我注意到它经常 return 1 小于应该 returned。当有 2 行时,它 returns 1,当有 1 行时它 returns nothing.
我附上了数据库结果的屏幕截图。
如有任何建议,我们将不胜感激。
[![<?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()
//
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]
在你的 cemetery
函数中你有一个 fetch
。这会将光标前进一个位置,因此当您返回主脚本并使用 fetch
时,您总是在第二行。
解决方案:
- 在函数中循环
fetch
并且只是 return 所有值的数组。
或
- 不要
fetch
函数和 return 结果集,这样你就可以 fetch
整个块。
我正在尝试从 mySQL 构建 API 到 return 的数据。我仍在努力掌握 PDO 的窍门。我有下面的查询,它应该 return 来自数据库的所有行,基于单个参数,但我注意到它经常 return 1 小于应该 returned。当有 2 行时,它 returns 1,当有 1 行时它 returns nothing.
我附上了数据库结果的屏幕截图。
如有任何建议,我们将不胜感激。
[![<?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()
//
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]
在你的 cemetery
函数中你有一个 fetch
。这会将光标前进一个位置,因此当您返回主脚本并使用 fetch
时,您总是在第二行。
解决方案:
- 在函数中循环
fetch
并且只是 return 所有值的数组。
或
- 不要
fetch
函数和 return 结果集,这样你就可以fetch
整个块。