views:

164

answers:

3
$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);
A: 

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
It depends on what `mysql_error` exactly does.I guess it probably won't bother MySQL to get error of last statement.
+1  A: 

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
A: 

Meant as a comment :/

Arkh