上半部分正常工作。 $selectedVerb
和$verbNum
都会恢复正常并显示出来。当我尝试在下一个查询中使用$verbNum
时,它会导致错误:
致命错误:未捕获错误:调用成员函数fetch_array() on boolean;堆栈跟踪:#0 {main}抛出。
代码:
if ($stmt = $dbc->prepare("SELECT verbNum FROM verblist WHERE mainVerb = ? ")){
$stmt->bind_param("s", $selectedVerb);
$stmt->execute();
$stmt->bind_result($verbNum);
$stmt->fetch();
echo '<span>'.$selectedVerb.' is part of verb number: '.$verbNum. '</span>';
settype($verbNum, "integer");
$verbListarray = mysqli_query($dbc, "SELECT mainVerb FROM verblist WHERE verbNum = $verbNum");
while($row = $verbListarray->fetch_array())
{
echo $row['mainVerb'];
echo "<br />";
}
$stmt->close();
}
答案 0 :(得分:1)
以程序方式混合OOP方式不是一个好方法,所以如下所示: -
if ($stmt = $dbc->prepare("SELECT verbNum FROM verblist WHERE mainVerb = ? ")){
$stmt->bind_param("s", $selectedVerb);
$stmt->execute();
$stmt->bind_result($verbNum);
$stmt->fetch();
echo '<span>'.$selectedVerb.' is part of verb number: '.$verbNum. '</span>';
//settype($verbNum, "integer");
$stmt->close();
$stmt = $dbc->prepare("SELECT mainVerb FROM verblist WHERE verbNum = ?");
$stmt->bind_param("i", $verbNum);
$stmt->execute();
while($row = $stmt->fetch()){
echo $row['mainVerb'];
echo "<br />";
}
$stmt->close();
}
答案 1 :(得分:1)
没有充分理由在循环中执行多个查询,只需加入两个查询。
$stmt = $dbc->prepare("
SELECT t1.verbNum, t2.mainVerb
FROM verblist AS t1
JOIN verblist AS t2 ON t1.verbNum = t2.verbNum
WHERE t1.mainVerb = ?
ORDER BY t1.verbNum")
$stmt->bind_param("s", $selectedVerb);
$stmt->execute();
$stmt->bind_result($verbNum, $mainVerb);
$firstRow = true;
while ($stmt->fetch()) {
if ($firstRow) {
echo '<span>'.$selectedVerb.' is part of verb number: '.$verbNum. '</span>';
$firstRow = false;
}
echo $mainVerb . "<br>";
}