tags:

views:

375

answers:

5

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 
}
+1  A: 

mysq_affected_rows

I understand that by saying "successful" you want to know if any rows has updated. You can check it with this function.

If you use PDO -> http://www.php.net/manual/en/pdostatement.rowcount.php

Balon
A: 
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

Tor Valamo
+1  A: 
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.

Chacha102
+1  A: 

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
}
streetparade
A: 
if ($DB->rowCount() > 0)
   {/* Update worked because query affected X amount of rows. */}
else
    {$error = $DB->errorInfo();}
Cian E