Ray
Ray

Reputation: 51

Query not running

I wonder if anyone can spot what is going wrong here? The first query has been tested and is working because I can print out the $groups array, but the 2nd one will not run. When I hard code the $groups array it runs fine.

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

    }
}

Upvotes: 0

Views: 63

Answers (2)

Noman
Noman

Reputation: 4116

Try removing @ with mysqli_query also $groups array will not start looping until your count is greater then zero. if you var_dump($count) it will return zero.

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

You should count array after the query returns result set, right after ending 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);

Upvotes: 0

SevStryker
SevStryker

Reputation: 151

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

$count is always equal zero

Upvotes: 3

Related Questions