umar
umar

Reputation: 3133

query execution error check

hello please help me out regarding this function

  function delete($serviceid) 
    {
    $result = $this->query=("delete from service where service_id='$serviceid'");
    $exe=$this->executeNonQuery();
    if ($exe){
    return $success = "Record deleted successfully.";
    } else {
    return $error = "Unable to process at this time.";
    }
    }

$exe is always 1 even if the record is not deleted as may b its because of , it only check that query is executed or not , how can i check if the record is deleted then show success message

Upvotes: 1

Views: 2870

Answers (3)

Brad
Brad

Reputation: 163334

I don't know what DB class you are using, but there are generally separate methods for accessing the number of affected rows, and whether or not the command was 'successful' (only from the viewpoint of the DB interface).

Look for something in your API documentation for number of affected rows or something similar.

If you could post more details, we can give you a specific answer.

EDIT: Now that we know you are using mysqli, you can use mysql->affected_rows() to get the number of rows deleted. See http://php.net/manual/en/mysqli.affected-rows.php

function affectedRows() {
    return mysqli_affected_rows($this->conn);
}

Add that to your "Person" class.

And then, switch to PDO and prepared statements. What you are doing is very insecure.

Upvotes: 1

psparrow
psparrow

Reputation: 9938

We definitely need more information on the class that $this is an instance of in order to know how it's query functions work. However, if that class is using a mysql connection, the mysql_affected_rows() function should return the number of rows that were deleted.

A quick test would be to substitute that function instead of checking the value of $exe:

if (mysql_affected_rows()){
   return $success = "Record deleted successfully.";
}

Check this out for more details: https://www.php.net/mysql_affected_rows

Upvotes: 1

xkeshav
xkeshav

Reputation: 54016

check for mysql_affected_rows()

Reference

Upvotes: 1

Related Questions