php $stmt->execute() :如何知道数据库插入是否成功?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9991882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 08:02:40  来源:igfitidea点击:

$stmt->execute() : How to know if db insert was successful?

phpoopmysqli

提问by cosmicsafari

With the following piece of code, how do i know that anything was inserted in to the db?

使用下面的一段代码,我怎么知道有什么东西被插入到数据库中了?

if ($stmt = $connection->prepare("insert into table (blah) values (?)")) {
$stmt->bind_param("s", $blah);  
$stmt->execute();           
$stmt->close();                                 
}

I had thought adding the following line would have worked but apparently not.

我原以为添加以下行会起作用,但显然不会。

if($stmt->affected_rows==-1){$updateAdded="N"; echo "failed";}  

And then use the $updatedAdded="N" to then skip other pieces of code further down the page that are dependent on the above insert being successful.

然后使用 $updatedAdded="N" 跳过依赖于上述插入成功的页面下方的其他代码段。

Any ideas?

有任何想法吗?

回答by Manse

The execute()method returns a boolean... so just do this :

execute()方法返回一个boolean... 所以只需这样做:

if ($stmt->execute()) { 
   // it worked
} else {
   // it didn't
}

回答by MattP

Check the return value of $stmt->execute()

检查 $stmt->execute() 的返回值

if(!$stmt->execute()) echo $stmt->error;

Note that line of code does perform the execute() command so use it in place of your current $stmt->execute() not after it.

请注意,代码行确实执行了 execute() 命令,因此请使用它代替当前的 $stmt->execute() ,而不是在它之后。

回答by álvaro González

Just check the manual pagesof whatever function you are using:

只需检查您正在使用的任何功能的手册页

prepare() - returns a statement object or FALSEif an error occurred.
bind_param() - Returns TRUEon success or FALSEon failure.
execute() - Returns TRUEon success or FALSEon failure.
close() - Returns TRUEon success or FALSEon failure.

prepare() - 返回一个语句对象或FALSE如果发生错误
bind_param() -TRUE成功或FALSE失败时返回。
execute() -TRUE成功或FALSE失败时返回。
close() -TRUE成功或FALSE失败时返回。

In practice, though, this gets annoying and it's error prone. It's better to configure mysqli to throw exceptions on errorand get rid of all specific error handling except for the few occasions where an error is expected (e.g., a tentative insert that might violate a unique constraint):

但是,在实践中,这很烦人,而且容易出错。最好将mysqli 配置为在错误时抛出异常并摆脱所有特定的错误处理,除了少数预期错误的情况(例如,可能违反唯一约束的临时插入):

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

回答by mishu

if you mean that you want to know the number of affected rows you can use rowCount on the pdo statement

如果你的意思是你想知道受影响的行数,你可以在 pdo 语句上使用rowCount

$stmt->rowCount();

after execute;

执行后;

if you are talking about error handling I think the best option is to set the errmode to throwing exteptions and wrap everything in a try/catch block

如果您在谈论错误处理,我认为最好的选择是将 errmode 设置为抛出 exteptions 并将所有内容包装在 try/catch 块中

try
{
    //----
}
catch(PDOException $e)
{
    echo $e->getMessage();
}

回答by CBEK

You can check the returned value after the execute :

您可以在执行后检查返回值:

if ($stmt->execute()) { 
    // ok :-)
    $count = $stmt->rowCount();
    echo count . ' rows updated properly!';
} else {
    // KO :-(
    print_r($stmt->errorInfo());
}

回答by Pedro Ant?nio

Other way:

另一种方式:

if ($stmt->error){
        echo "Error";
    }
    else{
        echo "Ok";
    }