Scan for next index

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
richie256
Forum Commoner
Posts: 37
Joined: Mon Oct 13, 2003 8:00 pm
Location: Montréal/Canada

Scan for next index

Post by richie256 »

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 :wink:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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 »

Can you not just edit the database scheme so that the field is auto incremented?
Post Reply