SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:



    PHP 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 
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    Animated Chatrooms - www.121chatrooms.net

  4. #4
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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());

  5. #5
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you so much
    Animated Chatrooms - www.121chatrooms.net

  6. #6
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    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
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  7. #7
    SitePoint Addict bronze trophy Hall of Famer's Avatar
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    370
    Mentioned
    6 Post(s)
    Tagged
    2 Thread(s)
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •