Ruben Urresti
Ruben Urresti

Reputation: 39

delete query doesn't work via php pdo

I am trying to use the following code:

$Q = DBCon::getCon()->prepare('SELECT * FROM `'.$language['data_table_name'].'` WHERE `id`=:id');
$Q->bindValue(':id', $recordId, PDO::PARAM_INT);
$Q->execute();
var_dump($Q);
var_dump($recordId);
var_dump($Q->fetch(PDO::FETCH_ASSOC));

$Q = DBCon::getCon()->prepare('DELETE FROM `'.$language['data_table_name'].'` WHERE `id`=:id');
$Q->bindValue(':id', $recordId, PDO::PARAM_INT);
$Q->execute();
var_dump($Q);
var_dump($recordId);

which generates the following data via var_dump:

object(PDOStatement)[6]
  public 'queryString' => string 'SELECT * FROM `project_nl` WHERE `id`=:id' (length=41)

string '5' (length=1)

array (size=4)
  'id' => int 5
  'name' => string 'test' (length=4)
  'description' => string 'test' (length=4)
  'big_image' => string 'test' (length=4)

object(PDOStatement)[6]
  public 'queryString' => string 'DELETE FROM `project_nl` WHERE `id`=:id' (length=39)

string '5' (length=1)

Which should, as far as I know, delete the record where id equals 5 from the following table:

|Table name: project_nl               |
---------------------------------------
| id | name | description | big_image |
---------------------------------------
| 1  | test | test        | test      |
| 5  | test | test        | test      |

However, the piece of code doesn't actually delete anything. Neither does it throw any errors/exceptions.

When I, however, copy the query from the var_dump and paste it into phpMyAdmin, the row does get deleted.

Also, the script is full of working insert, update and select queries which all work fine. It's only DELETE which doesn't do anything at all.

Upvotes: 2

Views: 979

Answers (2)

user6533277
user6533277

Reputation:

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // sql to delete a record
    $sql = "DELETE FROM MyGuests WHERE id=3";

    // use exec() because no results are returned
    $conn->exec($sql);
    echo "Record deleted successfully";
    }
catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }

$conn = null;
?>

Upvotes: 0

Ruben Urresti
Ruben Urresti

Reputation: 39

I disabled auto commit, and didnt start nor commit a transaction.

Upvotes: 1

Related Questions