Page 1 of 1

Scan for next index

Posted: Wed Oct 20, 2004 3:49 pm
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:

Posted: Wed Oct 20, 2004 4:03 pm
by Weirdan

Code: Select all

select max(no_client) FROM clients

Posted: Wed Oct 20, 2004 4:37 pm
by kettle_drum
Can you not just edit the database scheme so that the field is auto incremented?