开发者

Do you use mysql_error($con) or mysql_insert_id($con) to check insert result?

开发者 https://www.devze.com 2022-12-17 09:05 出处:网络
$dml = \"insert into table ...\"; mysql_query($dml,$con); The above insert something into a table.then you can check if it succeeded by either
$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 b开发者_StackOverflow中文版elow still have $id fetched when running both of them,I've not tried yet:

$err = mysql_error($con);
$id = mysql_insert_id($con);


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消