Php PDO,查询是否正确完成?
是否可以使用此方法检查插入(或其他类型的查询)是否成功完成Php PDO,查询是否正确完成?,php,mysql,pdo,sql-insert,Php,Mysql,Pdo,Sql Insert,是否可以使用此方法检查插入(或其他类型的查询)是否成功完成 $sql = 'INSERT INTO mytable (info, address, phone) VALUES (?,?,?)'; $sth = $conn->prepare($sql); if ($sth->execute(array($info, $address, $phone))) { // success!, now do something } 或者,即使没有插入任何内容,但准备/查询本身正常,它也
$sql = 'INSERT INTO mytable (info, address, phone) VALUES (?,?,?)';
$sth = $conn->prepare($sql);
if ($sth->execute(array($info, $address, $phone))) {
// success!, now do something
}
或者,即使没有插入任何内容,但准备/查询本身正常,它也会返回true吗?如果内部没有出现错误/警告,则返回值为true。它与查询的实际结果无关,只是意味着您的查询在语法上是正确的,并且没有打破任何数据库约束
换言之:如果您使用sql编辑器在数据库中成功运行查询,但0行受到影响,则在php中仍将返回true。是的,如果您使用了正确的方法,则可以使其更加详细,如下所示:
if ($sth->execute((array($info, $address, $phone))) {
// success!, now do something
echo 'lastInsertId = '.$conn->lastInsertId();
} else {
// Query failed.
echo 'errorcode = '.$sth->errorCode();
}
好的,所以我应该使用:
if($sth->rowCount()>0){//do something}
?好的。我只是检查lastinsertid是否为null?或者是数字+!空?他们还建议在注释中插入另一种方法输出…
,有趣的是,如果语句(“where something=something”)不匹配,我猜为什么要投否决票?请解释一下