need to test if sql query was successful

后端 未结 11 1099
遥遥无期
遥遥无期 2021-02-18 17:54

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

相关标签:
11条回答
  • 2021-02-18 18:12

    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
    }
    
    0 讨论(0)
  • 2021-02-18 18:14
    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

    0 讨论(0)
  • 2021-02-18 18:14

    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 
    }
    
    0 讨论(0)
  • 2021-02-18 18:23
    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.

    0 讨论(0)
  • 2021-02-18 18:23

    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
    }
    
    0 讨论(0)
  • 2021-02-18 18:29

    if the value is 0 then it wasn't successful, but if 1 then successful.

    $this->db->affected_rows();
    
    0 讨论(0)
提交回复
热议问题