How to get the last value (not the id) of a table ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tawfiq
Forum Newbie
Posts: 21
Joined: Sun Jan 27, 2008 12:19 pm

How to get the last value (not the id) of a table ?

Post by tawfiq »

I know mysql_insert_id() retrieves the last inserted ID, but I need to retrieve just the last value of a particular column. This table doesn't have any auto increment field. It only has one column - cart. I can't change the database stucture so cant add an auto increment field.

When I call the following function I don't get any value even though the value was just saved into the database.

Am I making any mistake when I say : $cart=mysql_result($result,0) ?

Is it not supposed to retrieve the last saved value?

Code: Select all

 
 
function getCart() {
 
    global $cart;
 
    
// if this is a new cart, then create a cart_no using the current time stamp.
// otherwise extract the cart_id from the session variable 
 
   if (!$cart)
   {
            
            
    $cart_id = date("d-m-Y-H-i-s");
            
    $sql ="INSERT INTO cart VALUES('".$cart_id."')";
    $result=mysql_query($new_cart_id_q);
    $cart=mysql_result($result,0);
            
    return $cart;
            
            
    }
        
    else
    {
                
         $cart= $_SESSION['cart'];
                
            
    }
        
        
        
        
}
 
 
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: How to get the last value (not the id) of a table ?

Post by Benjamin »

mysql_query will only return true or false for inserts.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: How to get the last value (not the id) of a table ?

Post by califdon »

A one-column table with no primary key is a no-no in a relational database. There is no way to identify a particular row. It violates the underlying rules that SQL is based upon.
Post Reply