user3702149
user3702149

Reputation: 17

PHP mysql return parameter when query result == true

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.

Upvotes: 0

Views: 53

Answers (2)

Rizier123
Rizier123

Reputation: 59681

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!";
    }

}

Upvotes: 0

KyleK
KyleK

Reputation: 5056

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.)

Upvotes: 1

Related Questions