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

目前,我将我的代码从 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) 但在这种情况下,我不知道它是否是解决方案,如果是,则在哪里以及如何放置它。


白猪掌柜的
浏览 172回答 1
1回答

慕的地8271018

以下是我的建议:mysqli_stmt_store_result()永远不要使用,除非您确切地知道它的用途。使用mysqli_stmt_get_result()来代替。无论如何 - 你不应该混合这两个功能。不要mysqli_stmt_num_rows()在结果上使用。使用mysqli_num_rows($result)来代替。切换到对象方法而不是全局函数调用。例如:$result->num_rows而不是mysqli_num_rows($result).&nbsp;代码会更短,您将无法使用参数类型错误的函数,例如mysqli_stmt_num_rows($result).使用MYSQLI_ASSOC代替MYSQLI_NUM。或使用mysqli_fetch_assoc().您的代码应该如下所示:$stmt = $conn->prepare("SELECT * FROM `assets`");$stmt->execute();$result = $stmt->get_result()if (!$result) {&nbsp;&nbsp; &nbsp; die("Could not show the required data");} elseif ($result->num_rows == 0) {&nbsp; &nbsp; echo "Currently there are no assets to be shown.";} else {&nbsp; &nbsp; echo '<table id="table" class="display"><thead> [..]';&nbsp; &nbsp; while($result2 = $result->fetch_assoc()) {&nbsp; &nbsp; &nbsp; &nbsp; $class="";&nbsp; &nbsp; &nbsp; &nbsp; if ($result2["Review_date"] < date('Y-m-d')){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $class=" old";&nbsp; &nbsp; &nbsp; &nbsp; } elseif($result2["Review_date"] < date('Y-m-d', strtotime('+6 days'))){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $class="notnew";&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $class="new";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; echo '<tr class="'.$class.'">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td>'.$result2["Asset_name"].'</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [..]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr>';&nbsp; &nbsp; }&nbsp; &nbsp; echo '</tbody></table>';}
打开App,查看更多内容
随时随地看视频慕课网APP