This topic is locked

MySQL Max() Function

11/18/2008 4:35:23 AM
PHPRunner General questions
C
cecotto479 author

I wonder if I'm missing something or doing something stupid. I'm trying to get the highest value of a field (it's in an "external" table and not part of the $values array) Running the sql directly brings up the correct result. No errors come up, it just doesn't provide the data requested. Any help would be gratefully received:-
function BeforeAdd(&$values,&$message,$inline)

{
$str = "SELECT users.email FROM users WHERE users.username = '".$_SESSION["OwnerID"]."'";

$rs = db_query($str,$conn);

$data = db_fetch_array($rs);

$email = $data["email"];
//The part above works perfectly
//The part below doesn't

$str1 = "SELECT Max(booking.BookingID) AS BookingID FROM booking";

$rs1 = db_query($str1,$conn);

$data1 = db_fetch_array($rs1);

$BookID = $data["BookingID"];

//everything below here works too - except the $BookID variable
$message="Your Classical Movement Quotation ID '$values[QuoteID]' has been booked. Your booking reference is:-". $BookID. "You can view the booking online.";

$subject="Thank you. Your Classical Movement booking is confirmed.";

$headers="From:booking@classicalmovement.co.uk"."\n\r";
foreach($values as $field=>$value)
$message.= "\n". $field." : ".$value. "\n";
mail($email, $subject, $message, $headers);

}

T
thesofa 11/18/2008

If BookingIDis geneated when the record is saved this will not exist yet because the record has not been saved, so it has no number.

If you are trying to get the number of the last booking, If you key field is auto-incremented use following code:

$Bookid = mysql_insert_id();


HTH

C
cecotto479 author 11/18/2008

If BookingIDis geneated when the record is saved this will not exist yet because the record has not been saved, so it has no number.

If you are trying to get the number of the last booking, If you key field is auto-incremented use following code:

$Bookid = mysql_insert_id();


HTH


Thanks, I'll give that a go, but I sort of understood that it didn't exist, yet, which is why I used Max(BookingID) to give me the number of the last Booking ID to be inserted?
EDIT:- Thanks, got what I wanted out of "mysql_insert_id()", so why the Max() bit didn't work is irrelevant. The "mysql_insert_id()" is much tidier too.