Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.
Moderator: General Moderators
richie256
Forum Commoner
Posts: 37 Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada
Post
by richie256 » Wed Oct 20, 2004 3:49 pm
Before insert a new record, I need to get the last Key (in this case, customer number) from a Table because it isn't Autoincrement. I have do this code. My question is: is there a faster way to code this... I am sure there is a better way!
Code: Select all
<?php
$result = mysql_query("SELECT no_client FROM clients");
if (!$result) {
echo 'Impossible d''exécuter la requête : ' . mysql_error();
exit;
}
$i = 0;
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($line as $col_value) {
if ($col_value > $i) {
$i = $col_value;
}
}
}
$i++;
?>
Thanks
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Oct 20, 2004 4:03 pm
Code: Select all
select max(no_client) FROM clients
kettle_drum
DevNet Resident
Posts: 1150 Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England
Post
by kettle_drum » Wed Oct 20, 2004 4:37 pm
Can you not just edit the database scheme so that the field is auto incremented?