brem13 Posted June 3, 2010 Share Posted June 3, 2010 i'm trying to figure out how to put data(users) into an array from a database, i'd also like to make it so that array doesnt have duplicates. any help? $result = mysql_db_query($database, "select * from $table ORDER BY rand()") or die (mysql_error()); while($qry = mysql_fetch_array($result)) { $user = $qry['username']; $pic = $qry['mainPic']; $lastOnline = $qry['lastLogin']; $lookingFor = $qry['lookingFor']; }//end while $users = array($user); $distinct = array_unique($users); Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/ Share on other sites More sharing options...
priti Posted June 3, 2010 Share Posted June 3, 2010 one more option can be: $users=array(); while($qry = mysql_fetch_array($result)) { if(!in_array($qry['username'],$users) { array_push($users,$qry['username']); } }//end while Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067216 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 ok, i tried that, its still giving me duplicates and all the usernames are 'array' now Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067218 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 I don't know why you'd have duplicate users in that table, but you just need to modify your SQL. Try adding a group by clause: select * from $table GROUP BY username ORDER BY rand() Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067220 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 nope, still duplicates, what i'm doing is displaying random users from a database(user is only in database once) but they show up multiple times on page Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067222 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 You're saying the query is selecting the exact same record twice? Does this happen if you run the same SQL separately in a MySQL administrator (e.g. PHPMyAdmin)? Edit: by the way mysql_db_query is deprecated. Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067227 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 yes, here is the full code while($i < 10) { if($i == 4 || $i == 7) echo "</tr><tr>"; echo "<td width=225px height=125px align=left valign=top>"; include("config.php"); mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); $result = mysql_db_query($database, "select * from $table GROUP BY username ORDER BY rand()") or die (mysql_error()); $users=array(); while($qry = mysql_fetch_array($result)) { $user = $qry['username']; $pic = $qry['mainPic']; $lastOnline = $qry['lastLogin']; $lookingFor = $qry['lookingFor']; if(!in_array($qry['username'],$users)) { array_push($users,$qry['username']); } }//end while list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic); if($height>=101) { echo "<a href=viewProfile.php?user=$users style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$users</font><br><img src=userImages/thumbs/$pic height=75px></a><br>"; }//end if $height else { echo "<a href=viewProfile.php?user=$users style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$users</font><br><img src=userImages/thumbs/$pic width=75px></a><br>"; } echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>"; echo "</td>"; $i++; }//end while Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067230 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 What are you saying yes to? Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067231 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 yes it is selecting the same record twice, here is a screenie of what it looks like [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067235 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 And you if run the SQL in a MySQL administrator? Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067238 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 it does not give me duplicates Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067241 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 Probably because you have the query contained in a loop.. while($i < 10) { Why is it within a loop? Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067249 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 if u notice in the picture, i want it to select 9 users randomly and put them in the table, without having to put the same code 9 times Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067250 Share on other sites More sharing options...
Adam Posted June 3, 2010 Share Posted June 3, 2010 select * from $table ORDER BY rand() LIMIT 9 Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067251 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 ok, tried that, and changed the code around a bit, took it out of the while loop and now i'm getting the same user for all of them modified code: $result = mysql_db_query($database, "select * from $table ORDER BY rand() LIMIT 9") or die (mysql_error()); while($qry = mysql_fetch_array($result)) { $user = $qry['username']; $pic = $qry['mainPic']; $lastOnline = $qry['lastLogin']; $lookingFor = $qry['lookingFor']; list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic); if($height>=101) { $display = "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic height=75px></a><br>"; }//end if $height else { $display = "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic width=75px></a><br>"; } }//end while $i = 1; while($i < 10) { if($i == 4 || $i == 7) echo "</tr><tr>"; echo "<td width=225px height=125px align=left valign=top>"; echo $display; echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>"; echo "</td>"; $i++; }//end while Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067259 Share on other sites More sharing options...
brem13 Posted June 3, 2010 Author Share Posted June 3, 2010 ok, finally got it to work, got rid of the while loop, inserted limit 9, changed around code a bit, result below mysql_connect($server, $db_user, $db_pass) or die (mysql_error()); $result = mysql_db_query($database, "select * from $table ORDER BY rand() LIMIT 9") or die (mysql_error()); $i=1; while($qry = mysql_fetch_array($result)) { $user = $qry['username']; $pic = $qry['mainPic']; $lastOnline = $qry['lastLogin']; $lookingFor = $qry['lookingFor']; list($width, $height, $type, $attr) = getimagesize("userImages/thumbs/".$pic); echo "<td width=225px height=125px align=left valign=top>"; if($height>=101) { echo "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic height=75px></a><br>"; }//end if $height else { echo "<a href=viewProfile.php?user=$user style='text-decoration:none;'><font face=tahoma size=2 color=#FFFFFF>$user</font><br><img src=userImages/thumbs/$pic width=75px></a><br>"; } echo "<font face=tahoma size=1>$lastOnline - $lookingFor</font>"; echo "</td>"; $rows = mysql_num_rows($result); if($i==3 || $i==6) echo "</tr><tr>"; $i++; }//end while Quote Link to comment https://forums.phpfreaks.com/topic/203763-put-into-an-array-from-database/#findComment-1067272 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.