mysql first record array

Discussion in 'Web Design and Development' started by jrock2004, Apr 23, 2009.

  1. jrock2004 macrumors 6502

    Joined:
    May 4, 2008
    Location:
    PA
    #1
    What I am trying to do is pull all the records in a table that have a value of 0 in the Seen column. Now in the database there is multiple times you see 0 in the see column. The way it is pulling it, it is giving the last instance when it sees it. I want the first record that shows the instance of 0 in Seen

    Code:
    $sql = "SELECT * FROM " .$table_name. " WHERE Seen = 0";
    	$results = @mysql_query($sql) or die(mysql_error());
    	while ($row = mysql_fetch_array($results))
        {
        	$subID = $row['subscriber_id'];
            $fname = $row['realtor_fname'];
            $lname = $row['realtor_lname'];
            $phone1 = $row['realtor_phone1a'];
            $phone2 = $row['realtor_phone1b'];
            $phone3 = $row['realtor_phone1c'];
            $timedate = $row['time_created'];
            $sugar = $row['sugarcrm_imported'];
        }
    
    
    Thanks
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    Not sure if I completely understand what you're wanting, but how about this,
    Code:
    $sql = "SELECT * FROM $table_name WHERE Seen = 0 ORDER BY time_created DESC LIMIT 1";
    I simply added an ordering based on creation time in descending order, and limited it to one result. I removed some of the concatenation as it wasn't necessary. Also, for the zero, depending how it's stored you may need "0" rather than just 0. And make sure the Seen column has an uppercase S as it may be case sensitive.
     
  3. SrWebDeveloper macrumors 68000

    SrWebDeveloper

    Joined:
    Dec 7, 2007
    Location:
    Alexandria, VA, USA
    #3
    @OP:

    BTW, instead of "mysql_fetch_array($results)" since you're obviously referencing the result set by an associative array, use either "mysql_fetch_assoc($results)" or "mysql_fetch_array($results,MYSQL_ASSOC)" (which is the same as the former).

    Otherwise your version will return both an associative array and number indices which is inefficient, unless desired.

    -jim
     

Share This Page