Jump to content

[SOLVED] MySQL Select (Hopefully Simple)


hoopplaya4

Recommended Posts

Hi all,

 

I am looking for some help on selecting variables from MySQL, but leaving out the oldest (or smallest numerical) value. 

 

Let's say I have several values (which are auto-incremented) in the DB.  For example:

 

32, 34, 35, 36, 38, 40.

 

When I select these ID's from the Database, I'd like to select and echo all of them except the one which was first created.  In the example above, it would be "32".

 

How would I go about achieving something like this?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/135948-solved-mysql-select-hopefully-simple/
Share on other sites

In your sql query do an order by ID, then the following should help you with the logic

 

$i=0;
while ($row = mysql_fetch_assoc($result)) {
    if ($i == 0) {
        $i++;
        continue; // dont print the first id
    }
    
     echo $row['id'];
}

 

Hope that helps ya.

@Flames:

 

The reason was because those numbers are dynamically generated.  Thus, I won't always know what the number is.  I was only using "32" as an example.  Sorry for not being clear.

 

 

@premiso:

 

That did it, thanks for getting me in the right direction.  However, I have one caveat, (and maybe I'm asking too much).

 

In order to pull it off, I had to ORDER BY ASC.  But, I'd actually like to display the variables in a descending fashion. 

 

Is this possible in my circumstance?

 

 

@Flames:

 

The reason was because those numbers are dynamically generated.  Thus, I won't always know what the number is.  I was only using "32" as an example.  Sorry for not being clear.

 

 

@premiso:

 

That did it, thanks for getting me in the right direction.  However, I have one caveat, (and maybe I'm asking too much).

 

In order to pull it off, I had to ORDER BY ASC.  But, I'd actually like to display the variables in a descending fashion. 

 

Is this possible in my circumstance?

 

 

 

Just reverse the logic:

 

$i=1;
$cnt = mysql_num_rows($result);
while ($row = mysql_fetch_assoc($result)) {
    if ($i == $cnt) {
        continue; // dont print the first id
    }
   
     echo $row['id'];
     $i++;
}

 

Should do the trick.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.