1

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

6
  • 3
    You need to provide more information. What is $this->query? Does it use builtin mysql functions? Are you using PDO? Are you using Mysqli? Commented Mar 23, 2011 at 19:38
  • The method name executeNonQuery seems to be used in database adapters for a few Microsoft technologies, FWIW. I found one horrible example PHP database adapter in Google. I hope that's not the one being used here. Commented Mar 23, 2011 at 19:41
  • The only thing I can think of is if he was calling one of those adapters over COM, but then we would see a COM method... Commented Mar 23, 2011 at 19:43
  • @umar, you should really use something else... you aren't escaping your data. This is horribly insecure. Try PDO. phpro.org/tutorials/Introduction-to-PHP-PDO.html Commented Mar 23, 2011 at 19:44
  • in the api its like this function executeNonQuery() //executes non queries { try { $check = $this->conn->query($this->query); } catch(Exception $e) { $this->error = "An Error Occurred, while executing a Non Query"; $this->edetail = $e->getMessage(); return false; } return true; } Commented Mar 23, 2011 at 19:47

3 Answers 3

1

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.

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

2 Comments

The Api function looks like this function executeNonQuery() //executes non queries { try { $check = $this->conn->query($this->query); } catch(Exception $e) { $this->error = "An Error Occurred, while executing a Non Query"; $this->edetail = $e->getMessage(); return false; } return true; }
@umar, that isn't helpful. What DB layer are you using?
1

check for mysql_affected_rows()

Reference

3 Comments

if u r using any class for mysql then there must be a function to check the The number of rows in a result set on success . u shoud try that.
This won't work, as he isn't using mysql_query(), so he doesn't have a valid resource to pass to mysql_num_rows().
for a delete you should use mysql_affected_rows() to find out how many rows were affected.
1

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

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.