1
$dml = "insert into table ...";
mysql_query($dml,$con);

The above insert something into a table.then you can check if it succeeded by either

if('' == mysql_error($con))...

or

if($id = mysql_insert_id($con))...

What's your choice and reason?

BTW,will the below still have $id fetched when running both of them,I've not tried yet:

$err = mysql_error($con);
$id = mysql_insert_id($con);
skaffman
  • 398,947
  • 96
  • 818
  • 769
user198729
  • 61,774
  • 108
  • 250
  • 348

2 Answers2

1

I use mysql_error. I do so b/c it is more consistent with error checking in other parts of the program, and I will know why the error occurred instead of just receiving a false value.

RC.
  • 27,409
  • 9
  • 73
  • 93
0

mysql_error() will always throw an error if the insert operation fails, so fetching the insert_id is not really necessary as far as I can see, unless you need it for further processing.

Your code block should work, however the manual recommends to run insert_id() directly after issuing the query.

Note: Because mysql_insert_id() acts on the last performed query, be sure to call mysql_insert_id() immediately after the query that generates the value.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • It depends on what `mysql_error` exactly does.I guess it probably won't bother MySQL to get error of last statement. – user198729 Jan 26 '10 at 12:39