JJohnsenDK Posted January 2, 2007 Share Posted January 2, 2007 HeyI 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]<?phpinclude('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øndbyMarie Petersen[/i][code]<?phpinclude('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 JohnsenMarie 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? Quote Link to comment https://forums.phpfreaks.com/topic/32562-solved-how-call-mysql_fetch_array-more-than-once/ Share on other sites More sharing options...
wildteen88 Posted January 2, 2007 Share Posted January 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/32562-solved-how-call-mysql_fetch_array-more-than-once/#findComment-151404 Share on other sites More sharing options...
JJohnsenDK Posted January 2, 2007 Author Share Posted January 2, 2007 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$row2and so on if i wanted to so the mysql_fetch_array function several times :)But thanks for your help :D Quote Link to comment https://forums.phpfreaks.com/topic/32562-solved-how-call-mysql_fetch_array-more-than-once/#findComment-151415 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.