DaveTheMinion
DaveTheMinion

Reputation: 664

PHP - Echo All Results From MySQL SELECT Query

I have a PHP script that executes a MySQL SELECT query, and I would like to echo each row from that query to the page. I have tried multiple methods to go about doing this, but none seems to work. The most reasonable of these ways that I have come up with is provided below, but it doesn't work either.

$result = $conn->query("SELECT count FROM countTable;") or trigger_error($conn->error);
$row = $result->fetch_array(MYSQL_BOTH);
$count = sizeof($row);
for($i = 0; $i <= $count; $i++)
{
    echo $row[$i];
}

The code provided above only prints the first row resulting form the query. How can I modify this code so that it prints the entirething?

Upvotes: 0

Views: 1939

Answers (1)

Gregable
Gregable

Reputation: 498

The array returned from fetch_array is the set of columns from one row of results. Your query has only one column (ie: "count"), so the size of the array will always be 1. Calling fetch_array multiple times will return each row one at a time.

What you want is something like:

while ($row = $result->fetch_array(MYSQL_BOTH)) {
   echo $row[0];
}

*Note, I haven't run this, so there might be some small syntax error I've overlooked.

Upvotes: 1

Related Questions