从mysqli_query获取成功/失败响应 [英] get success/failure response from mysqli_query
问题描述
我正在从用户那里收集信息,然后将它们添加到表中.
I'm gathering info from a user and then adding them to a table.
$insert = "INSERT INTO jos_activeagents (RINGPHONE, AGENTUID, FNAME, LNAME) VALUES ('(618) 717-2054','".$result['AGTBRDIDMM']."','".$result['AGTFNAME']."','".$result['AGTLNAME']."')";
$set = mysqli_query($link,$insert);
AGENTUID
是唯一键.如果用户尝试使用重复的唯一密钥提交,我会收到一条错误消息(当然).
AGENTUID
is a unique key. If a user tries to submit with a duplicate unique key, I get an error (of course).
现在,我该如何知道是否以及何时发生错误,然后将响应返回到页面呢?我知道mysqli_get_warnings()
,但是PHP手册没有显示任何示例.
Now, how would I go about knowing if and when an error occurred and then putting a response back to the page? I know of mysqli_get_warnings()
, but the PHP manual doesn't show any examples.
我也尝试过先在表中查找AGENTUID
:
I have also tried looking for the AGENTUID
in the table first:
$check = "SELECT * FROM jos_activeagents WHERE AGENTUID = '".$agt."'";
$runcheck = mysqli_query($link,$check);
$rescheck = mysqli_fetch_assoc($runcheck);
if($rescheck != null){
echo 'This Agent ID is already enrolled.'
}
但这似乎草率.有更好的方法吗?
But this seems sloppy. Is there a better way do this?
推荐答案
您可以使用 mysqli_error()
查看是否发生错误
You can use mysqli_error()
to see if an error occurred
if (mysqli_error($runcheck ))
{
// an error eoccurred
}
在您的特定示例中,最好在插入之前 检查行是否存在.您的示例很接近,但最好使用 mysqli_num_rows()
:
In your particular example you're better of checking if the row exists before doing the insert. Your example is close but would better using mysqli_num_rows()
:
$check = "SELECT * FROM jos_activeagents WHERE AGENTUID = '".$agt."'";
$runcheck = mysqli_query($link,$check);
if (mysqli_num_rows($runcheck) > 0)
{
// username in use
}
这篇关于从mysqli_query获取成功/失败响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!