0

In php I call a procedure in mysql. Now I want to check if a query result == true then set return variable in my php file in true or false if query failed. How can I do that?

This is my php code:

public function DeleteSingleAccount($accountId)
{
        $Config = new Config();
        $mysqli = $Config->OpenConnection();            
        mysqli_query($mysqli, "CALL DeleteSingleAccount($accountId)") or die("Query fail: ". mysqli_error());
}

This is my query in mysql for now:

DELETE 
    FROM table
        WHERE accountId = par_AccountId

This code runs correct but I want to set an return parameter when query is true or false.

2
  • What do you actually want to check? Whether a row was deleted, that the query executed or that the query was valid at all? Commented Dec 13, 2014 at 9:34
  • 1
    Assign the query like this: $result = mysqli_query... and the you can check it like this: if($result){ echo "win"; } else { echo "fail"; } (BTW: it can return true, but doesn't mean it has any row's init) Commented Dec 13, 2014 at 9:35

2 Answers 2

1
public function DeleteSingleAccount($accountId)
{
    $Config = new Config();
    $mysqli = $Config->OpenConnection();            
    return !! mysqli_query($mysqli, "CALL DeleteSingleAccount(" . intval($accountId) . ")");
}

I added intval() to avoid SQL injection (not needed if you're sure $accountId is always an integer, never null, empty string, user input, etc.)

Sign up to request clarification or add additional context in comments.

Comments

0

Think your looking for something like this:

public function DeleteSingleAccount($accountId) {
    $Config = new Config();
    $mysqli = $Config->OpenConnection();            
    $result = mysqli_query($mysqli, "CALL DeleteSingleAccount($accountId)") or die("Query fail: ". mysqli_error());
   //^Assignment of the query to a variable

    if($result && mysqli_num_rows($result) > 0) {
     //^Check if query is successfull      ^ Check if query has 1 or more rows in it! (You can delete this check if you don't care about how many row's the result has)
        echo "The query is successful and have 1 or more rows in it!";
    } else {
        echo "The query failed or have 0 rows in it!";
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.