Page 1 of 1

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

Posted: Sun Feb 10, 2008 3:58 pm
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'];
                
            
    }
        
        
        
        
}
 
 

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

Posted: Sun Feb 10, 2008 4:21 pm
by Benjamin
mysql_query will only return true or false for inserts.

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

Posted: Sun Feb 10, 2008 5:29 pm
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.