Jump to content

[SOLVED] How call mysql_fetch_array more than once?


JJohnsenDK

Recommended Posts

Hey

I was wondering how i can call mysql_fetch_array function more than once so the function doesnt pull out the first array the second time a call the mysql_fetch_array?

Example:
[code]
<?php
include('config.php');
$sql = "SELECT game.game_no, game.home_id, game.visitor_id, game.player_id, game.home_goals, game.home_red, game.home_yellow, player.fname, player.lname, team.name
FROM game
INNER JOIN player ON game.player_id = player.player_id
LEFT JOIN team ON game.home_id = team.team_id
WHERE game.game_no = ".$_GET['game']."";
$result = mysql_query($sql);

$row = mysql_fetch_array($result);

echo $row['name']."<br /><br /><br />";

while($row = mysql_fetch_array($result)){
echo $row['fname']." ".$row['lname']."<br />";
}
?>
[/code]

This code prints:
[i]Brøndby


Marie Petersen[/i]


[code]
<?php
include('config.php');
$sql = "SELECT game.game_no, game.home_id, game.visitor_id, game.player_id, game.home_goals, game.home_red, game.home_yellow, player.fname, player.lname, team.name
FROM game
INNER JOIN player ON game.player_id = player.player_id
LEFT JOIN team ON game.home_id = team.team_id
WHERE game.game_no = ".$_GET['game']."";
$result = mysql_query($sql);

echo $row['name']."<br /><br /><br />";

while($row = mysql_fetch_array($result)){
echo $row['fname']." ".$row['lname']."<br />";
}
?>
[/code]

This code prints:

[i]Jesper Johnsen
Marie Petersen[/i]

Because i dont want this [i]echo $row['name']."<br /><br /><br />";[/i] to loop throgh the while loop i have to put it outside, but that also results in that i have to write the [i]$row = mysql_fetch_array($result)[/i] twice. This causes that the second mysql_fetch_array doesnt read in the first array which is pulled out by the first time i call the fetch_array, i quess?

Anyone who can help out?
What are you trying to do I do not understand? Also don't use the same variable names. Use something different like so:
[code]$row2 = mysql_fetch_array($result);

echo $row2['name']."<br /><br /><br />";[/code]
Otherwise you are overwritting the variables. Also note anything your place out side of the while loop will only be run once. So the following  code:
[code]$row = mysql_fetch_array($result);

echo $row['name']."<br /><br /><br />";[/code]
Will only display the first row return from the mysql_query. Whereas the while loop will loop through the result set allowing you to get 1 row from the result set on each repetition of the loop.
I also only want to echo and line of code ones  ;)

But thanks you answered my questions. Which basicly was if i have to my more variables:

$row
$row2
and so on

if i wanted to so the mysql_fetch_array function several times  :)

But thanks for your help  :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.