calmchess Posted May 18, 2013 Share Posted May 18, 2013 Why is this mysql query only returning the last row of data? I can't remember how to write this correctly. Please help. <?php session_start();$docID=7768;if($docID===7768){__putreg($docID);}function __putreg($docID){if($docID===7768){ //check database for credentials $conn0=mysqli_connect("localhost","calmchess","ptoK4ptoK4","book");if($conn0){$q0=mysqli_query($conn0,"select _logged_in_picpath,_logged_in_username from logged_in_users")or die(mysqli_error($conn0));while($row = mysqli_fetch_array($q0)){$picpaths0[]=$row['_logged_in_picpath'];$usernames0[]=$row['_logged_in_username'];} for($i=0;$i<count($picpaths0);$i++){$picspaths00 = $picpaths0[$i].":::";$picassets0=$picspaths00."^^^".$usernames0[$i];if($i/4==0){$picassets0=$picassets0."xxx";}if($i==count($picpaths0)-1){ echo $picassets0;}}} }} ?> Link to comment https://forums.phpfreaks.com/topic/278140-query-only-returns-the-last-row-from-database/ Share on other sites More sharing options...
Jessica Posted May 18, 2013 Share Posted May 18, 2013 Because you're only echoing it once, after everything is done. You have two loops, you only need one, and you should echo within the loop. Link to comment https://forums.phpfreaks.com/topic/278140-query-only-returns-the-last-row-from-database/#findComment-1430865 Share on other sites More sharing options...
calmchess Posted May 18, 2013 Author Share Posted May 18, 2013 yeah you are correct Here How I solved it <?php session_start(); $docID=7768; if($docID===7768){__putreg($docID);} function __putreg($docID){ if($docID===7768){ //check database for credentials $conn0=mysqli_connect("localhost","calmchess","ptoK4ptoK4","book"); if($conn0){ $q0=mysqli_query($conn0,"select _logged_in_picpath,_logged_in_username from logged_in_users")or die(mysqli_error($conn0)); $j=-1; while($row = mysqli_fetch_array($q0)){ $j++; $picpaths0[]=$row['_logged_in_picpath']; $usernames0[]=$row['_logged_in_username']; $picspaths00 = $picpaths0[$j].":::"; $picassets0=$picspaths00."^^^".$usernames0[$j]; if($j/4==0){ $picassets0=$picassets0."xxx"; } echo $picassets0; } } } } ?> Link to comment https://forums.phpfreaks.com/topic/278140-query-only-returns-the-last-row-from-database/#findComment-1430867 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.