获取准备好的语句中的行数
Get amount of rows in prepared statement
我正在使用通过字符串连接构建的准备好的语句查询:
$sqlitemsparse
-“Select”子句
$sqlitemsparse .
- “Where”子句
$sqlitemsparse2
- “限制”选项
这按预期工作。
现在,另外,我需要 $sqlitemsparse
和 $sqlitemsparse .
查询的结果行数(没有“LIMIT”选择)。
我尝试将 mysqli_num_rows 函数与新的 mysqli_query 结合使用,但出现以下错误:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in...
代码:
$conn = new mysqli("localhost", "xxxx", "xxxx", "xxxx");
// Variable to bind
$classid = "4";
$sqlitemsparse = "SELECT * FROM itemSparse INNER JOIN item ON item.id = itemSparse.id";
$sqlitemsparse.= " WHERE item.ClassID = ?";
$sqlitemsparse2 = " LIMIT 0, 10";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sqlitemsparse . $sqlitemsparse2)) {
echo "SQL Failed";
} else {
mysqli_stmt_bind_param($stmt, "s", $classid);
mysqli_stmt_execute($stmt);
$resultitemsparse = mysqli_stmt_get_result($stmt);
while($rowitemsparse = mysqli_fetch_assoc($resultitemsparse)) {
$rowsitemsparse[] = $rowitemsparse;
}
}
$number_filter_row = mysqli_num_rows(mysqli_query($conn, $sqlitemsparse));
如果你想获得与同一个准备语句匹配但没有 LIMIT
的行数,那么你需要执行另一个准备语句,但这次使用 SELECT COUNT(*)
而不是 SELECT *
.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli("localhost", "xxxx", "xxxx", "xxxx");
// Variable to bind
$classid = "4";
$sqlitemsparse = "SELECT * FROM itemSparse INNER JOIN item ON item.id = itemSparse.id";
$sqlitemsparse .= " WHERE item.ClassID = ?";
$sqlitemsparse2 = " LIMIT 0, 10";
$stmt = $conn->prepare($sqlitemsparse . $sqlitemsparse2);
$stmt->bind_param('s', $classid);
$stmt->execute();
$resultitemsparse = $stmt->get_result();
$rowsitemsparse = $resultitemsparse->fetch_all(MYSQLI_ASSOC);
$sqlitemsparse = "SELECT COUNT(*) FROM itemSparse INNER JOIN item ON item.id = itemSparse.id";
$sqlitemsparse .= " WHERE item.ClassID = ?";
$stmt = $conn->prepare($sqlitemsparse);
$stmt->bind_param('s', $classid);
$stmt->execute();
$resultitemsparse = $stmt->get_result();
$row_count = $resultitemsparse->fetch_row()[0]; // first column of first row
我正在使用通过字符串连接构建的准备好的语句查询:
$sqlitemsparse
-“Select”子句$sqlitemsparse .
- “Where”子句$sqlitemsparse2
- “限制”选项
这按预期工作。
现在,另外,我需要 $sqlitemsparse
和 $sqlitemsparse .
查询的结果行数(没有“LIMIT”选择)。
我尝试将 mysqli_num_rows 函数与新的 mysqli_query 结合使用,但出现以下错误:
Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, bool given in...
代码:
$conn = new mysqli("localhost", "xxxx", "xxxx", "xxxx");
// Variable to bind
$classid = "4";
$sqlitemsparse = "SELECT * FROM itemSparse INNER JOIN item ON item.id = itemSparse.id";
$sqlitemsparse.= " WHERE item.ClassID = ?";
$sqlitemsparse2 = " LIMIT 0, 10";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sqlitemsparse . $sqlitemsparse2)) {
echo "SQL Failed";
} else {
mysqli_stmt_bind_param($stmt, "s", $classid);
mysqli_stmt_execute($stmt);
$resultitemsparse = mysqli_stmt_get_result($stmt);
while($rowitemsparse = mysqli_fetch_assoc($resultitemsparse)) {
$rowsitemsparse[] = $rowitemsparse;
}
}
$number_filter_row = mysqli_num_rows(mysqli_query($conn, $sqlitemsparse));
如果你想获得与同一个准备语句匹配但没有 LIMIT
的行数,那么你需要执行另一个准备语句,但这次使用 SELECT COUNT(*)
而不是 SELECT *
.
<?php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$conn = new mysqli("localhost", "xxxx", "xxxx", "xxxx");
// Variable to bind
$classid = "4";
$sqlitemsparse = "SELECT * FROM itemSparse INNER JOIN item ON item.id = itemSparse.id";
$sqlitemsparse .= " WHERE item.ClassID = ?";
$sqlitemsparse2 = " LIMIT 0, 10";
$stmt = $conn->prepare($sqlitemsparse . $sqlitemsparse2);
$stmt->bind_param('s', $classid);
$stmt->execute();
$resultitemsparse = $stmt->get_result();
$rowsitemsparse = $resultitemsparse->fetch_all(MYSQLI_ASSOC);
$sqlitemsparse = "SELECT COUNT(*) FROM itemSparse INNER JOIN item ON item.id = itemSparse.id";
$sqlitemsparse .= " WHERE item.ClassID = ?";
$stmt = $conn->prepare($sqlitemsparse);
$stmt->bind_param('s', $classid);
$stmt->execute();
$resultitemsparse = $stmt->get_result();
$row_count = $resultitemsparse->fetch_row()[0]; // first column of first row