PHP MySql Query into Array

Discussion in 'PHP' started by oo7ml, Aug 19, 2008.

  1. #1
    Hi, i have a table that has three fields:

    - username (primary key)
    - date (when they joined)
    - status (active, inactive, pending)

    My table (called accounts) has over 300 users and i want a report that shows the last 7 users who are active. Therefore i need a query that puts the last 7 active users usernames into an array.

    I have tried a tutorial on this but i can't seem to get it working. Can any of the experienced users help me with this (using my values)

    I think i have the query right but i don't really know how to complete the entire script

    $query_lastseven = "SELECT * FROM accounts WHERE status = 'active' ORDER BY date DESC LIMIT 7";
    PHP:
    thanks in advance to anyone who can help me complete this script correctly, thanks
     
    oo7ml, Aug 19, 2008 IP
  2. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #2
    the SQL query looks right...

    what format are the dates in the date field?
     
    lanmonkey, Aug 19, 2008 IP
  3. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Hi,

    thanks for replying, the dates are listed as: 2008-08-19 16:49:39

    thanks
     
    oo7ml, Aug 19, 2008 IP
  4. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Add 'or die(mysql_error())' as shown below:
    
    $resource=mysql_query($sql) or die(mysql_error());
    
    Code (SQL):
    Also make sure that you are iterating correctly through the resource. The correct way would be:
    
    $sql="SELECT username,date FROM accounts WHERE status='active' ORDER BY date DESC LIMIT 0,7";
    $resource=mysql_query($sql) or die(mysql_error());
    while($array=mysql_fetch_assoc($resource))
    {
    $username=$array['username'];
    $date=$array['date'];
    //rest of the code
    }
    
    Code (PHP):
    Also, in your SQL statement, you actually don't need to select the 'status' field because it will always be 'active' as you are conditionally choosing only those records where status='active'!
     
    rohan_shenoy, Aug 19, 2008 IP
  5. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    I have tried what you said, however only one client is listed instead of seven
    
    	$sql="SELECT MainClientID, datefirstcontact FROM tblclients WHERE status='live' ORDER BY datefirstcontact DESC LIMIT 0,7";
    	
    	$resource=mysql_query($sql) or die(mysql_error());
    	
    	while($array=mysql_fetch_assoc($resource))
    	
    	{
    	$aClientList=$array['MainClientID'];
    	}
    
    
    
        // assign the list for your template
        $this->oSmarty->assign('aList', $aClientList);
    PHP:
    any ideas
     
    oo7ml, Aug 20, 2008 IP
  6. Freewebspace

    Freewebspace Notable Member

    Messages:
    6,213
    Likes Received:
    370
    Best Answers:
    0
    Trophy Points:
    275
    #6
    what is "datefirstcontact "? Is it date or something else

    and whether you have executed this query in database first ? and in a sample php file? ahd whether you checked the results?
     
    Freewebspace, Aug 20, 2008 IP