0

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

0

4 Answers 4

2

You do it like this:

$results = array();
While ($row = mysql_fetch_assoc($result))
{
$results[] = $row;
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

1 Comment

@nkr thought that obvious. Edited.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.