所以我的主机没有为 mysqli 安装 mysqlnd 驱动程序,所以我无法使用 get_result()
函数(并且它不是安装 mysqlnd 驱动程序的选项)。以下查询的替代方案是什么,以便我不使用 get_result()
?
我知道我可以使用以下方法从数据库获取单个结果:
if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `count_id` = ?")) {
$stmt->bind_param('i', $count_id);
$stmt->execute();
$stmt->bind_result($count);
$stmt->fetch();
$stmt->close();
}
但是如果我必须从数据库中选择多个结果,循环遍历每个结果并获得另一个结果怎么办?
if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `number` = ?")) {
$stmt->bind_param('i', $number);
$stmt->execute();
$result = $stmt->get_result(); // uses get_result()
}
if ($result->num_rows) { // uses get_result()
while ($row = $result->fetch_assoc()) { // uses get_result()
if($stmt = $cxn->prepare("SELECT `value` FROM `values` WHERE `number` = ?")) {
$stmt->bind_param('i', $row['count']);
$stmt->execute();
$stmt->bind_result($value);
$stmt->fetch();
$stmt->close();
}
}
}
我无法使用它,因为某些语句需要 get_result()
我无法使用。
我有什么选择?
最佳答案
试试这个,
if ($stmt = $cxn->prepare("SELECT `count` FROM `numbers` WHERE `count_id` = ?"))
{
$stmt->bind_param('i', $count_id);
$stmt->execute();
$result = $stmt->fetchAll();
if ( count($result) )
{
foreach($result as $row)
{
print_r($row);
}
}
else
{
echo "Nothing Returned.";
}
}
关于php - get_result() 函数的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21560141/