查询未运行

时间:2015-08-27 14:24:32

标签: php sql

我想知道是否有人能发现这里出了什么问题?第一个查询已经过测试并且正在运行,因为我可以打印出$ groups数组,但第二个查询将无法运行。当我对$ groups数组进行硬编码时,运行正常。

require ('mysqli_connect.php'); // Connect to the db.
echo '<p>If no record is shown, this is because you had an incorrect or missing entry in the search form.<br>Click the back button on the browser and try again</p>';

//Coming from another page
$uninum=$_POST['uninum'];
$sname=$_POST['sname'];

$groups = array ( );
$count = count ($groups);

$q1 = "SELECT `groupid` FROM `groups` 
         WHERE `uninum` = '".$uninum."'";
$result1 = @mysqli_query($dbcon,$q1);
while ($row1 = mysqli_fetch_array ($result1, MYSQLI_ASSOC)){

    $groups[] = $row1['groupid'];

}


for ($i = 0; $i < $count; $i++){

    $q = "SELECT participants.sname, participants.uninum, 
                 groups.groupid 
          FROM participants 
             INNER JOIN groups ON 
                participants.uninum = groups.uninum 
          WHERE groups.groupid ='".$groups[$i]."'";         

    $result = @mysqli_query ($dbcon, $q); // Run the query.
    if ($result) { // If it ran, display the records.
        // Table header.
        echo '<table>
        <tr>
            <td><b>Edit</b></td>
            <td><b>Surnname</b></td>
            <td><b>University ID</b></td>
            <td><b>Group</b></td>
        </tr>';
        // Fetch and display the records:
        while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
            echo '<tr>
            <td><a href="edit_group_member.php?uninum=' . $row['uninum'] . '">Edit</a></td>
            <td>' . $row['sname'] . '</td>
            <td>' . $row['uninum'] . '</td>
            <td>' . $row['groupid'] . '</td>
            </tr>';
        }
        echo '</table>'; // Close the table.
        mysqli_free_result ($result); // Free up the resources. 
        echo "<br><br>";
    } else { // If it did not run OK.
        // Public message:
        echo '<p class="error">The current users could not be retrieved. We apologize for any inconvenience.</p>';
        // Debugging message:
        echo '<p>' . mysqli_error($dbcon) . '<br><br>Query: ' . $q . '</p>';

    }
}

2 个答案:

答案 0 :(得分:3)

$groups = array ( );
$count = count ($groups);

$ count总是等于零

答案 1 :(得分:0)

尝试删除@ mysqli_query同时$groups数组将不会开始循环,直到您的计数大于零。 如果你var_dump($count),它将返回零。

$count = count ($groups);
var_dump($count);

count返回结果集之后,您应该query数组,在结束while loop之后

$q1 = "SELECT `groupid` FROM `groups` WHERE `uninum` = '".$uninum."'";
$result1 = @mysqli_query($dbcon,$q1);
while ($row1 = mysqli_fetch_array ($result1, MYSQLI_ASSOC)){
        $groups[] = $row1['groupid'];
}
// count here 
$count = count ($groups);