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
}
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_query
in the backend (whatever $DB->query()
uses to query the database), it will return a TRUE
or FALSE
for INSERT
, UPDATE
, and DELETE
(and a few others), commands, based on their status.
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
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
}
if ($DB->rowCount() > 0)
{/* Update worked because query affected X amount of rows. */}
else
{$error = $DB->errorInfo();}
if the value is 0 then it wasn't successful, but if 1 then successful.
$this->db->affected_rows();
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
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--->
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
}
You can use this to check if there are any results ($result) from a query:
if (mysqli_num_rows($result) != 0)
{
//results found
} else {
// results not found
}
Source: Stackoverflow.com