PHP looping problem

I have a php loop code to display todays logins on my website. Is it possible to display a username only once, instead of multiple times. Here is my code:


// Start listing todays member chatters
  $result = mysql_db_query($dbname, "SELECT * FROM logs_logins WHERE date = '$showdate' AND ismember = '1' order by id desc") or die (mysql_error());
echo "<br /><b>$alang146</b><br />";
if (mysql_num_rows($result)) {
   while ($qry = mysql_fetch_array($result)) {

	   echo "$qry[username] <a href='admin.php?act=logsuserresults&loguser2=$qry[username]&logtype2=member&logtype3=get'>$alang103</a><br />";
   }  // End listing active chatters
}  // End listing todays guest chatters

Too easy mate.

Change:
$result = mysql_db_query($dbname, “SELECT * FROM logs_logins WHERE date = ‘$showdate’ AND ismember = ‘1’ order by id desc”) or die (mysql_error());

To:
$result = mysql_db_query($dbname, “SELECT DISTINCT(username),* FROM logs_logins WHERE date = ‘$showdate’ AND ismember = ‘1’ order by id desc”) or die (mysql_error());

The other options is to group, but I think using a DISTINCT is more efficient.

I suggest also ordering by date if that is a datetime or unixtimestamp field.

seem to get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘* FROM logs_logins WHERE date = ‘20/08/2013’ AND ismember = ‘1’ order by id desc’ at line 1

Sorry:

$result = mysql_db_query($dbname, “SELECT DISTINCT(username) as username FROM logs_logins WHERE date = ‘$showdate’ AND ismember = ‘1’ order by id desc”) or die (mysql_error());

thank you so much :slight_smile:

One thing to note is that the mysql_* is deprecated as of the current version of PHP and is likely to be removed in the next version. You should be migrating over to at least the mysqli_* extension, or to PDO, which is better as it’s easier to switch to another db server software

Ditto to what SpacePhoenix said. Its about time to move on from mysql deprecated functions, and to move on with either MySQLi or PDO. Moreover, Id say try using OOP with database management, once you get used to it you wont ever want to let go.