Search code examples
phpmysqldelete-row

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!


Solution

  • Applied to innodb tables as in your case.

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