Al Hennessey
Al Hennessey

Reputation: 2445

Variable for each result in WHILE loop SELECT query

I have a question about PHP WHILE loops. I know that you can perform the action mysql_num_rows to get the number of results from a SELECT query.

But say I have 3 results come through the select query and i put them through a while loop, how do I assign a variable to the number each result is in the mysql_num_row.

Such as
1 for result 1
2 for result 2
3 for result 3.
Sorry if this is a bit confusing, but i can't seem to find an easy way to explain it.

Thanks

Upvotes: 0

Views: 1142

Answers (4)

Tieran
Tieran

Reputation: 1036

There are a few 'new' methods for pulling back data from MySQL now, if you're just starting out this might be the ideal time to have a look at them. This looks like a good place to start: http://www.php.net/manual/en/mysqlinfo.api.choosing.php.

Upvotes: 0

Liam Bailey
Liam Bailey

Reputation: 5905

You do it like this:

$results = array();
While ($row = mysql_fetch_assoc($result))
{
$results[] = $row;
}

Upvotes: 2

seeker
seeker

Reputation: 3333

$res=array();
$i=0;
While(mysql_fetch_array(...)){
$i++;
$res[$i]=value
//do logic
}

Upvotes: 0

Chris
Chris

Reputation: 4370

$myArray = array();
while($row = mysql_fetch_assoc($result)) {
    $myArray[] = $row
}

Upvotes: 0

Related Questions