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
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
}
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
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
}
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.
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 the value is 0 then it wasn't successful, but if 1 then successful.
$this->db->affected_rows();