php 需要测试sql查询是否成功
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2060325/
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
need to test if sql query was successful
提问by Brad
I have this query and if it returns successful, I want another function to process, if not, do not process that function.
我有这个查询,如果它返回成功,我想要另一个函数来处理,如果没有,不要处理那个函数。
Here is the code for running the query
这是运行查询的代码
global $DB;
$DB->query("UPDATE exp_members SET group_id = '$group_id' WHERE member_id = '$member_id'");
I imagine it is something like...
我想它是这样的......
if($DB) {
//success
} else {
//failure
}
回答by streetparade
This is the simplest way you could test
这是您可以测试的最简单方法
$query = $DB->query("UPDATE exp_members SET group_id = '$group_id' WHERE member_id = '$member_id'");
if($query) // will return true if succefull else it will return false
{
// code here
}
回答by kjagiello
I understand that by saying "successful" you want to know if any rows have been updated. You can check it with this function.
我知道通过说“成功”,您想知道是否有任何行已更新。您可以使用此功能进行检查。
If you use PDO -> http://www.php.net/manual/en/pdostatement.rowcount.php
如果您使用 PDO -> http://www.php.net/manual/en/pdostatement.rowcount.php
回答by Tyler Carter
global $DB;
$status = $DB->query("UPDATE exp_members SET group_id = '$group_id' WHERE member_id = '$member_id'");
if($status == false)
{
die("Didn't Update");
}
If you are using mysql_queryin the backend (whatever $DB->query()uses to query the database), it will return a TRUEor FALSEfor INSERT, UPDATE, and DELETE(and a few others), commands, based on their status.
如果您mysql_query在后端$DB->query()使用(无论用于查询数据库的任何内容),它将 根据其状态返回一个TRUE或FALSEfor INSERT、UPDATE、 和DELETE(以及其他一些)命令。
回答by Cian E
if ($DB->rowCount() > 0)
{/* Update worked because query affected X amount of rows. */}
else
{$error = $DB->errorInfo();}
回答by tucker
if the value is 0 then it wasn't successful, but if 1 then successful.
如果值为 0 则不成功,但如果值为 1 则成功。
$this->db->affected_rows();
回答by Hyman Briner
This has proven the safest mechanism for me to test for failure on insert or update:
这已证明是我测试插入或更新失败的最安全机制:
$result = $db->query(' ... ');
if ((gettype($result) == "object" && $result->num_rows == 0) || !$result) {
failure
}
回答by Brian Powell
if you're not using the ->format, you can do this:
如果您不使用该->格式,则可以执行以下操作:
$a = "SQL command...";
if ($b = mysqli_query($con,$a)) {
// results was successful
} else {
// result was not successful
}
回答by Raghu Dhumpati
Check this:
检查这个:
<?php
if (mysqli_num_rows(mysqli_query($con, sqlselectquery)) > 0)
{
echo "found";
}
else
{
echo "not found";
}
?>
<!----comment ---for select query to know row matching the condition are fetched or not--->
回答by Reza Baradaran Gazorisangi
You can use this to check if there are any results ($result) from a query:
您可以使用它来检查查询是否有任何结果 ($result):
if (mysqli_num_rows($result) != 0)
{
//results found
} else {
// results not found
}
回答by Tor Valamo
if ($DB->query(...)) { success }
else { failure }
query should return false on failure (if you're using mysql_query or $mysqli->query). If you want to test if the UPDATE query actually did anything (which is a totally different thing than "successful") then use mysql_affected_rows or $mysqli->affected_rows
查询失败时应返回 false(如果您使用的是 mysql_query 或 $mysqli->query)。如果你想测试 UPDATE 查询是否真的做了任何事情(这与“成功”完全不同)然后使用 mysql_affected_rows 或 $mysqli->affected_rows

