我正在尝试使用以下代码:
$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时,该行会被删除。
此外,该脚本还包含有效的insert
,update
和select
个查询。它只是DELETE
,它根本不做任何事情。
答案 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;
?>