删除查询无法通过php pdo工作

时间:2016-10-18 23:17:21

标签: php mysql pdo sql-delete

我正在尝试使用以下代码:

$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);

通过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)

据我所知,应从下表中删除id等于5的记录:

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

但是,这段代码实际上并没有删除任何内容。它也不会抛出任何错误/异常。

但是,当我从var_dump复制查询并将其粘贴到phpMyAdmin时,该行会被删除。

此外,该脚本还包含有效的insertupdateselect个查询。它只是DELETE,它根本不做任何事情。

2 个答案:

答案 0 :(得分:0)

我禁用了自动提交,并且没有启动也没有提交事务。

答案 1 :(得分:0)

<?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;
?>