class MyDb{
.......
public function test($sql)
{
$this->affect_nums = $this->pdo->exec($sql);//影响行数
if ($this->affect_nums>0) {
$this->insertId = $this->pdo->lastInsertId();//最后插入id
if(null !== $this->insertId){
echo "插入成功。";
} else {
echo "修改/删除成功。";
}
} else {
$error = $this->pdo->errorInfo();
echo $error[2];
}
}
}
$MyDb = MyDb::getInstance();
//表结构test2((auto_increment,primary key),name,password);
$MyDb->test("INSERT INTO test2(name,password) values('小华','123')");
echo $MyDb->affect_nums.",".$MyDb->insertId;
echo "<br>";
$MyDb->test("UPDATE test2 SET name='曾华' WHERE name='小华'");
echo $MyDb->affect_nums.",".$MyDb->insertId;
echo "<br>";
$MyDb->test("DELETE FROM test2 WHERE name='曾华'");
echo $MyDb->affect_nums.",".$MyDb->insertId;
抱歉,代码中的lastInsertid()少了两个括号,复制的时候不知道为什么少了,问题不是这个...