need to test if sql query was successful


Question

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
18
1/13/2010 9:24:30 PM

Accepted Answer

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
}
30
1/13/2010 9:19:51 PM

mysql_affected_rows

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


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon