PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

Im only getting one set of result when trying to use mysql_fetch_array()

function GetVideoInfo( $video_id, $user_id ) 
{
    $result = mysql_query("SELECT * FROM `mytable` 
                            WHERE 
                                video_id = '$video_id' 
                            AND 
                                user_id = '$user_id'") 
                                or die( mysql_error() );

        return mysql_fetch_array( $result );
}

$videoRecepients = $viddler_custom->GetVideoRecepients( $video_details['id'] );

echo "<pre>";
print_r($videoRecepients);
echo "</pre>"

When I try using print_r, it only results a single row in the table. My expected result should have 2 results. I am 100% sure that my query is correct, so that is not the problem. I'm thinking that maybe it's on my mysql_fetch_array that is wrong.
Your help would be greatly appreciated and rewarded! Thanks! :)

Upvotes: 0

Views: 90

Answers (3)

rogerlsmith
rogerlsmith

Reputation: 6786

You need to put the mysql_fetch_array($result) in a loop

while($row = mysql_fetch_array($result))
{
   // do something with $row
}

Upvotes: 1

Derek Downey
Derek Downey

Reputation: 1532

From the example on the manual page, mysql_fetch_array returns the information on the current pointer of the $result object. This will mean you want to loop through the result until you've fetched everything.

while ($row=mysql_fetch_array($result)) {
    $set[] = $row;
}
print_r($set);

Upvotes: 1

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

All of the fetch functions will return a single row, you will need to loop until the result is empty like this (snippet from php.net).

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    printf("ID: %s  Name: %s", $row["id"], $row["name"]);
}

Upvotes: 2

Related Questions