Subhash
Subhash

Reputation: 175

Delete row from MySQL not working

I'm trying to delete a row from a table in MySQL using the PHP code below. The return value from mysql_affected_rows() is 1. There are no errors. However, the row still exits in MySQL. Not sure what I'm doing wrong. Any thoughts?

$db = array('host'=>'127.0.0.1',  
        'user'=>'root',  
        'pass'=>'',  
        'name'=>'testdb');

// CONNECT TO THE MYSQL SERVER
$connection = mysql_connect($db['host'], $db['user'], $db['pass']);
if(!$connection){
    // HANDLE ERROR HERE
    die('Unable to connect to MySql server : '.mysql_error($connection));
}

// SELECT THE DATABASE SCHEMA
if(!mysql_select_db($db['name'],$connection)){
    // HANDLE ERRORS HERE
    die('Unable to connect to database : '.mysql_error($connection));
}

$result = mysql_query("delete from photos where id=".$photo_id, $connection);
echo mysql_affected_rows($connection);

UPDATE

I added the following code to the end and that solved the issue -

mysql_query("commit", $connection);

Thanks for the comments!

Upvotes: 1

Views: 1679

Answers (2)

shinguz
shinguz

Reputation: 11

What I typically do in this cases is to enable the general_log (set global general_log_file=general.log; and set global general_log=1;) and look what arrives in there.

Then it often becomes already obvious what is the problem.

If it is not clear yet what is going on you can run these queries on the mysql console...

Upvotes: 0

Igor Parra
Igor Parra

Reputation: 10348

Applied to innodb tables as in your case.

mysql_query("BEGIN", $connection); 
// delete code
mysql_query("COMMIT", $connection);

Upvotes: 1

Related Questions