Can't get mysql_affected_rows to confirm delete operation

I am having trouble creating a confirmation that a delete was successful. Here is the code:

$conn = db_connect();
$result = $conn->query(“delete from products where sku=‘1234’”);
if (!$result) {
throw new Exception(‘Error’);
}

The code works perfectly to delete the product, but it never returns an error message if it is unable to delete the product (e.g. if the SKU doesn’t exist).

I found online that I should use mysql_affected_rows so I tried this without success:

$conn = db_connect();
$result = $conn->query(“delete from products where sku=‘1234’”);
if (mysql_affected_rows() == 0) {
throw new Exception(‘Error’);
}

The error I get is:
Warning: mysql_affected_rows() [function.mysql-affected-rows]: Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’ (2) in /home/content/24/10297924/html/secure/user_auth_fns.php on line 188

Out of desperation I also tried mysql_affected_rows($result) and mysql_affected_rows($conn) but I get errors:
mysql_affected_rows() expects parameter 1 to be resource, boolean given…
or
mysql_affected_rows() expects parameter 1 to be resource, object given…

Since I am obviously not understanding how this works I figured someone here surely knows more than me and can help me out.

Thanks very much.

What does this function look like? db_connect()

I’m guessing here (since I can’t see the function) but it looks like mysqli

If that’s the case you would use $conn->affected_rows

Thanks for the reply M@tt. The db_connect() function looks like this:
$result = new mysqli($host, $user, $pass, $database);
if (!$result) {
throw new Exception(‘Could not connect to database server’);
} else {
return $result;
}

Your recommendation did not throw an error, so that is a good sign, but it returned 0 even though the delete took place (so a row was affected and I would have thought it should return 1).

Any thoughts as to why this is? Here is how I call it :

$conn = db_connect();
$result = $conn->query(“delete from products where sku=‘1234’”);
if ($conn->affected_rows == 0) {
throw new Exception(‘Error’);
}

When I simply try to echo ($conn->affected_rows); it returns 0 even when a row is affected.

If you have any idea what is going on thanks for taking the time to let me know.

That should work according to the manual.

http://php.net/manual/en/mysqli.affected-rows.php

Unfortunately it doesn’t. Any thoughts as to why?

You will just need to debug. Are you certain there are no errors and that at least 1 row is being deleted?

[php]
$conn = db_connect();
if ($result = $conn->query(“delete from products where sku=‘1234’”)) {
printf(“Affected rows (UPDATE): %d\n”, $conn->affected_rows);
}
else {
printf(“Error: %s\n”, $conn->error);
}
[/php]

Thanks very much for your help Matt. I played around with it some more and eventually it properly returned the number of affected rows (not sure what the issue was to be honest).

Thanks again for your time.

Sponsor our Newsletter | Privacy Policy | Terms of Service