gpt4 book ai didi

php - SQL 查询从 mysql 转换为过程式错误 : Commands out of sync; you can't run this command now

转载 作者:行者123 更新时间:2023-11-29 05:48:33 25 4
gpt4 key购买 nike

目前我将我的代码从 mysqli 样式编辑为 mysqli_stmt,以便能够更好地防止 SQL 注入(inject)。我重写了我的代码,但是当我想检查某个查询有多少行时,我收到此错误:命令不同步;你现在不能运行这个命令,所以我的表不会显示任何数据,但实际上它有。

我已经读了几个小时这个错误,但我不知道如何调整它才能使其正常工作。

此外,我知道在这种情况下我没有变量,但我倾向于在我的所有代码中使用相同的样式。

这是我目前使用的代码(无法正常工作):

$stmt = mysqli_prepare($conn,"SELECT * FROM `assets`");
mysqli_stmt_execute($stmt);
mysqli_stmt_store_result($stmt);

if (!($result = mysqli_stmt_get_result($stmt))){
die("Could not show the required data");}
elseif (empty(mysqli_stmt_num_rows($result))){
echo "Currently there are no assets to be shown.";}
elseif (mysqli_stmt_num_rows($result) > 0){
echo '<table id="table" class="display"><thead>
<tr>
<th>Asset name</th>
<th>Classification</th>
<th>Tag</th>
<th>Department</th>
<th>Next review date</th>
<th>Owner</th>
<th>Update</th>
</tr></thead><tbody>';

while($result2=mysqli_fetch_array($result, MYSQLI_NUM))
{ $class="";
if ($result2["Review_date"] < date('Y-m-d')){$class=" old";} elseif($result2["Review_date"] < date('Y-m-d', strtotime('+6 days'))){$class="notnew";} else {$class="new";}
echo '<tr class="'.$class.'">
<td>'.$result2["Asset_name"].'</td>
<td>'.$result2["Classification"].'</td>
<td>'.$result2["Tag"].'</td>
<td>'.$result2["Department"].'</td>
<td>'.$result2["Review_date"].'</td>
<td>'.$result2["Responsible"].'</td>
<td><a href="editassets.php?id='.$result2['id'].'">Edit</a> | <a href="deleteassets.php?id='.$result2['id'].'" onclick="return confirm(\'Do you want to delete this asset?\');">Delete</a></td>
</tr>';
}
echo '</tbody></table>';
}

我知道我的问题出在第一个 elseif 语句上,我尝试使用 mysqli_stmt_free_result($stmt) 但在这种情况下我不知道它是否是解决方案,如果是的话,在哪里以及如何放置

感谢您的帮助!

最佳答案

以下是我的建议:

  1. 永远不要使用mysqli_stmt_store_result(),除非您确切地知道它有什么用。请改用 mysqli_stmt_get_result()。无论如何 - 你不应该混合这两个功能。
  2. 不要对结果使用 mysqli_stmt_num_rows()。请改用 mysqli_num_rows($result)
  3. 改用对象方法而不是全局函数调用。例如:$result->num_rows 而不是 mysqli_num_rows($result)。代码会更短,您将无法使用参数类型错误的函数,例如 mysqli_stmt_num_rows($result)
  4. 使用 MYSQLI_ASSOC 而不是 MYSQLI_NUM。或者使用 mysqli_fetch_assoc()

您的代码应该类似于:

$stmt = $conn->prepare("SELECT * FROM `assets`");
$stmt->execute();
$result = $stmt->get_result()

if (!$result) {
die("Could not show the required data");
} elseif ($result->num_rows == 0) {
echo "Currently there are no assets to be shown.";
} else {
echo '<table id="table" class="display"><thead> [..]';

while($result2 = $result->fetch_assoc()) {
$class="";
if ($result2["Review_date"] < date('Y-m-d')){
$class=" old";
} elseif($result2["Review_date"] < date('Y-m-d', strtotime('+6 days'))){
$class="notnew";
} else {
$class="new";
}
echo '<tr class="'.$class.'">
<td>'.$result2["Asset_name"].'</td>
[..]
</tr>';
}
echo '</tbody></table>';
}

关于php - SQL 查询从 mysql 转换为过程式错误 : Commands out of sync; you can't run this command now,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57125314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com