1

Hey guys I was wondering how I would get the highest id in wordpress. So for example I have a database that a guest will check out and if they check out each time with the same email address I want it to grab the highest id becuase the highest id would be the most recent entry.

Code:

$id = $wpdb->get_var( $wpdb->prepare( 
                "
                    SELECT ID 
                    FROM wp_discount_info_guest 
                    WHERE email = %s
                ", 
                $email
            ) );

I get the value 2 because in the database field id 2 holds the email that is stored in the var $email

So I want it to grab the highest id value if there is another entry because that would mean the highest id is the most recent.

Let me know if you need more.

thanks!

David Biga
  • 2,763
  • 8
  • 38
  • 61
  • What exactly are you trying to achieve by capturing the email ID of the last guest..? Also what do you mean by '...I have a database that a guest will check out ..'? – Kent Pawar Dec 09 '12 at 04:26
  • Possible duplicate : http://stackoverflow.com/questions/2659253/how-to-select-the-last-record-from-mysql-table-using-sql-syntax – Kent Pawar Dec 09 '12 at 04:28
  • You have to be careful with the other guys post because I'm in wordpress and they are a little anal about other ways in calling the database. – David Biga Dec 09 '12 at 20:07

2 Answers2

2

You can use this to get the max ID for the email .

select max(ID) FROM wp_discount_info_guest  WHERE email = %s
kannanrbk
  • 6,964
  • 13
  • 53
  • 94
2

try this

$id = $wpdb->get_var( $wpdb->prepare( 
            "
                SELECT ID 
                FROM wp_discount_info_guest 
                WHERE email = %s
                ORDER BY ID DESC limit 0,1
            ", 
            $email
        ) );
Pragnesh Chauhan
  • 8,363
  • 9
  • 42
  • 53