Query problem - return info with - and without - loop

Hi everyone,

I have a query which doesn’t work.

<?php
require (MYSQL);
$q = "SELECT col1, col2 FROM table1";
$r = @mysqli_query($dbc, $q);

$num = mysqli_num_rows($r);
if ($num > 0) {
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
Print'<p>print something – no loop</p>';

while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
	
Print'<p>print something else – must be looped</p>';

}
}

?>

Some of the information returned from the database needs to be printed out without using a loop, and some information must be looped.

Would the above query work at all, or must I use 2 separate queries?

Thanks a lot for your advice.

Let’s start by adding an error check to find out why your query doesn’t work. That’s a necessary step. There’s nothing obvious in your query statement, so it’s probably your connection. Do you have error checking turned on in your connection code?

After that - why does it matter if you grab the query results using a loop or not? YOu have to get all the data somehow… IF you use the array retrieval then you still have to loop/walk through the array to see the results. Or am I missing something?

Hi ginerjm,

thanks for replying. It seems that the code does work to some extent. The first block to be echoed out works fine but for some reason the second block only returns 3 rows, when it should be returning 4 rows. The row that isn’t being returned is the first row. When I remove the first block, save, and refresh the page, all four rows in the second block are returned.

Not sure if there’s some conflict.

Hi RedBishop,

You should be able to use the mysqli_data_seek function to reset the result pointer back to the first record, so you can loop over the result set from the beginning:


$query = "SELECT col1, col2 FROM table1";
$result = @mysqli_query($dbc, $query);

$num = mysqli_num_rows($result);

if ($num > 0)
{
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
    Print '<p>print something – no loop</p>';

    mysqli_data_seek($result, 0);

    while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
    {
        Print '<p>print something else – must be looped</p>';
    }
}

So - your query WAS working, your problem was in displaying the results. Fretburner has provided you the solution to your real problem.

We would be remiss in not pointing out that the “MySQL” extension is deprecated (not ‘depreciated’ as so many people spell it) and you should really focus on choosing one of the other options - mysqli or pdo.

RedBishop is already using the mysqli_* functions… good point about the spelling though, I always get that one wrong.

Hi fretburner,

thank you for replying and I hope you’re well!

I have yet to try out your solution because of other queries giving me trouble.

Will keep you posted.

Cheers

funny how one can miss that little tiny i … oh, well.

Hi there fretburner,

the mysqli_data_seek function has solved the problem. :tup: Thanks a lot!

Your advice is always helpful.