如何检查插入成功与否当我使用此代码时?
我想检查插入成功与否
如果成功echo "success";
别的echo "error";
$link = mysql_connect($host, $user, $pass);
$sql = mysql_query("INSERT INTO `transaction` (txnid, pro_name, price, status)
VALUES (
'".$data['txn_id']."' ,
'".$data['pro_name']."' ,
'".$data['price']."' ,
'$status'
)", $link);
return mysql_insert_id($link);
答案 0 :(得分:1)
简单地:
if (!$sql) echo 'error';
else echo 'success';
来自mysql_query上的文档:
For ... INSERT ... mysql_query()成功时返回TRUE,错误时返回FALSE。
答案 1 :(得分:0)
$link = mysql_connect($host, $user, $pass);
$sql = mysql_query("INSERT INTO `transaction` (txnid, pro_name, price, status)
VALUES (
'".$data['txn_id']."' ,
'".$data['pro_name']."' ,
'".$data['price']."' ,
'$status'
)", $link);
//------------Changes----------------
$inserted_id = mysql_insert_id($link);
if(mysql_errno() != 0){
echo "error";
else
echo "success";
return $inserted_id;
答案 2 :(得分:0)
根据mysql_insert_id
。它会在成功插入时返回新生成的id。
点击此处:
$link = mysql_connect($host, $user, $pass);
$sql = mysql_query("INSERT INTO `transaction` (txnid, pro_name, price, status)
VALUES (
'".$data['txn_id']."' ,
'".$data['pro_name']."' ,
'".$data['price']."' ,
'$status'
)", $link);
$lastInsertedId = mysql_insert_id($link);
if (empty($lastInsertedId))
$return = 'error';
else
$return = $lastInsertedId;
return $return;