简体   繁体   English

MySQL查询可在工作台中运行,但无法通过PHP接口运行

[英]MySQL query working in workbench, but does not work via PHP interface

I have a MySQL query that I know to be working in Workbench/Sequel Pro that appears to return empty in my PHP code: 我有一个MySQL查询,该查询知道在Workbench / Sequel Pro中工作,在我的PHP代码中似乎返回了空:

$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
            WHERE staffID='$inputID'
            AND invigilation.examID=exam.exam_ID
            AND exam.exam_ID=occupation.examID
            AND room.room_ID=occupation.roomID
            AND exam.module_ID=module.module_ID
            AND staff.password='$inputPassword'";
            $result = mysql_query($SQL);

            if($db_field = mysql_fetch_assoc($result)) {

            echo "
            <table border='1'>
                <tr>
                    <th>Exam ID</th>
                    <th>Module ID</th>
                    <th>Module name</th>
                    <th>Duration</th>
                    <th>Start time</th>
                    <th>Room</th>
                </tr>";



                while ($db_field = mysql_fetch_assoc($result)) {
                echo "
                <tr>
                    ";
                    echo "<td>" . $db_field['exam_ID'] . "</td>";
                    echo "<td>" . $db_field['module_ID'] . "</td>";
                    echo "<td>" . $db_field['module_name'] . "</td>";
                    echo "<td>" . $db_field['duration'] . "</td>";
                    echo "<td>" . $db_field['start_datetime'] . "</td>";
                    echo "<td>" . $db_field['room_name'] . "</td>";

                    echo "
                </tr>";
                } 

            } else {
                    echo "Incorrect login details. Please try again";
                    die();
                }

The result of this code is the first table row (Exam ID, Module ID etc.) is displayed but the table content is not. 该代码的结果是显示表格的第一行(考试ID,模块ID等),但不显示表格内容。

I cannot work out why this is happening. 我不知道为什么会这样。 Other than the SQL query, the code is identical to code used elsewhere (where it is working fine). 除了SQL查询外,该代码与在其他地方(可以正常工作)使用的代码相同。

You are doing mysql_fetch_assoc twice . 两次执行mysql_fetch_assoc The first time it reads the record and prints the table header. 第一次读取记录并打印表头。 The second time there are no more records so it doesn't print a row. 第二次没有更多记录,因此它不打印行。

Change 更改

if($db_field = mysql_fetch_assoc($result)) {

to

if(mysql_num_rows($result) > 0) {

Try this. 尝试这个。 You are using mysql_fetch_assoc($result); 您正在使用mysql_fetch_assoc($result); two times, First time it get result and second time there is no result. 两次,第一次得到结果,第二次没有结果。

 $SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
                WHERE staffID='$inputID'
                AND invigilation.examID=exam.exam_ID
                AND exam.exam_ID=occupation.examID
                AND room.room_ID=occupation.roomID
                AND exam.module_ID=module.module_ID
                AND staff.password='$inputPassword'";
                $result = mysql_query($SQL);
    $db_field = mysql_fetch_assoc($result)
                if($db_field != "" ) {

                echo "
                <table border='1'>
                    <tr>
                        <th>Exam ID</th>
                        <th>Module ID</th>
                        <th>Module name</th>
                        <th>Duration</th>
                        <th>Start time</th>
                        <th>Room</th>
                    </tr>";



                    while ($db_field) {
                    echo "
                    <tr>
                        ";
                        echo "<td>" . $db_field['exam_ID'] . "</td>";
                        echo "<td>" . $db_field['module_ID'] . "</td>";
                        echo "<td>" . $db_field['module_name'] . "</td>";
                        echo "<td>" . $db_field['duration'] . "</td>";
                        echo "<td>" . $db_field['start_datetime'] . "</td>";
                        echo "<td>" . $db_field['room_name'] . "</td>";

                        echo "
                    </tr>";
                    } 

                } else {
                        echo "Incorrect login details. Please try again";
                        die();
                    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM