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?
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.
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.