返回数组和 num 行 mysqli 准备
Returning array and num rows mysqli prepared
我对 mysqli 准备语句有点陌生,我想使用 fetch_array 到 return 结果以及 return num_rows 作为数组值。
我有这样的东西
function getCategories($dbh, $catId)
{
$data = array();
$s = "SELECT id, title FROM categories WHERE parent_id = ?";
if ($stmt = mysqli_prepare($dbh, $s)) {
mysqli_stmt_bind_param($stmt, "i", $catId);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_errno($stmt)) {
exit(mysqli_stmt_error($stmt));
}
mysqli_stmt_store_result($stmt);
$count = mysqli_stmt_num_rows($stmt)) {
if ($count) {
$data['count'] = $count;
$result = mysqli_stmt_get_result($stmt);
while ($r = mysqli_fetch_assoc($result)) {
$data[] = $r;
}
}
return $data;
} else {
exit(mysqli_error($dbh));
}
}
看来我不能使用mysqli_stmt_store_result和mysqli_stmt_get_result()。
store_result 函数似乎给出了一个布尔值,然后我得到了这个错误:"mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given"
希望这是有道理的。非常感谢任何帮助。
已更新:
function getCategories($dbh, $catId)
{
$data = array();
$s = "SELECT id, title FROM categories WHERE parent_id = ?";
if ($stmt = mysqli_prepare($dbh, $s)) {
mysqli_stmt_bind_param($stmt, "i", $catId);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_errno($stmt)) {
exit(mysqli_stmt_error($stmt));
}
$result = mysqli_stmt_get_result($stmt);
$data['count'] = $result->num_rows;
while ($r = mysqli_fetch_assoc($result)) {
$data[] = $r;
}
return $data;
} else {
exit(mysqli_error($dbh));
}
}
根据 PHP 文档,mysqli_stmt_get_result returns FALSE
错误:
Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure. The mysqli_errno() function can be used to distinguish between the two types of failure.
然后您将其传递给 mysqli_fetch_assoc
,它会抱怨,因为您给它一个布尔值而不是它期望的结果集。
到时候多做一些错误检查,你会没事的。您的 SQL 查询可能有问题。调用 mysqli_errorno 以确定是否存在错误,如上面的文档所述。
编辑:
使用 mysqli_error 函数获取 mysql 错误的描述。最好在检查失败的任何地方使用它,因为有错误消息将使调试比简单地静默失败更容易。
我对 mysqli 准备语句有点陌生,我想使用 fetch_array 到 return 结果以及 return num_rows 作为数组值。
我有这样的东西
function getCategories($dbh, $catId)
{
$data = array();
$s = "SELECT id, title FROM categories WHERE parent_id = ?";
if ($stmt = mysqli_prepare($dbh, $s)) {
mysqli_stmt_bind_param($stmt, "i", $catId);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_errno($stmt)) {
exit(mysqli_stmt_error($stmt));
}
mysqli_stmt_store_result($stmt);
$count = mysqli_stmt_num_rows($stmt)) {
if ($count) {
$data['count'] = $count;
$result = mysqli_stmt_get_result($stmt);
while ($r = mysqli_fetch_assoc($result)) {
$data[] = $r;
}
}
return $data;
} else {
exit(mysqli_error($dbh));
}
}
看来我不能使用mysqli_stmt_store_result和mysqli_stmt_get_result()。
store_result 函数似乎给出了一个布尔值,然后我得到了这个错误:"mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given"
希望这是有道理的。非常感谢任何帮助。
已更新:
function getCategories($dbh, $catId)
{
$data = array();
$s = "SELECT id, title FROM categories WHERE parent_id = ?";
if ($stmt = mysqli_prepare($dbh, $s)) {
mysqli_stmt_bind_param($stmt, "i", $catId);
mysqli_stmt_execute($stmt);
if (mysqli_stmt_errno($stmt)) {
exit(mysqli_stmt_error($stmt));
}
$result = mysqli_stmt_get_result($stmt);
$data['count'] = $result->num_rows;
while ($r = mysqli_fetch_assoc($result)) {
$data[] = $r;
}
return $data;
} else {
exit(mysqli_error($dbh));
}
}
根据 PHP 文档,mysqli_stmt_get_result returns FALSE
错误:
Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure. The mysqli_errno() function can be used to distinguish between the two types of failure.
然后您将其传递给 mysqli_fetch_assoc
,它会抱怨,因为您给它一个布尔值而不是它期望的结果集。
到时候多做一些错误检查,你会没事的。您的 SQL 查询可能有问题。调用 mysqli_errorno 以确定是否存在错误,如上面的文档所述。
编辑:
使用 mysqli_error 函数获取 mysql 错误的描述。最好在检查失败的任何地方使用它,因为有错误消息将使调试比简单地静默失败更容易。