为了账号安全,请及时绑定邮箱和手机立即绑定

SQL 查询从 mysql 转换到程序样式错误:命令不同步;你现在不能运行这个命令

SQL 查询从 mysql 转换到程序样式错误:命令不同步;你现在不能运行这个命令

PHP
白猪掌柜的 2021-10-22 10:37:51
目前,我将我的代码从 mysqli 样式编辑为 mysqli_stmt,以便能够更好地防止 SQL 注入。我重写了我的代码,但是当我想检查某个查询有多少行时,我收到了这个错误:命令不同步;你现在不能运行这个命令,所以我的表不会显示任何数据,但实际上它有。我已经读了几个小时关于这个错误,但我不知道如何调整它才能使它工作。此外,我知道在这种情况下我没有变量,但我倾向于在我的所有代码中使用相同的样式。这是我目前使用的代码(不起作用):$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 回答

?
慕的地8271018

TA贡献1796条经验 获得超4个赞

以下是我的建议:

  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>';

}


查看完整回答
反对 回复 2021-10-22
  • 1 回答
  • 0 关注
  • 171 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信