class MyDb{
..........
public function test($sql)
{
$this->affect_nums = $this-> ;pdo->exec($sql);//Number of rows affected
if ($this->affect_nums>0) {
$this->insertId = $this->pdo-> lastInsertId();//Last insert id
if(null !== $this->insertId){
echo "Insert successfully.";
} else {
echo "Modify/Delete Success.";
}
} else {
$error = $this->pdo->errorInfo();
}
} else {
}
}
$MyDb = MyDb::getInstance();
//Table structure test2((auto_increment,primary key),name,password);
$MyDb ->test("INSERT INTO test2(name,password) values('Xiaohua','123')");
echo $MyDb->affect_nums.",".$MyDb->insertId;
echo "<br>";
$MyDb->test("UPDATE test2 SET name='Zeng Hua' WHERE name='Xiaohua'");
echo $MyDb-> affect_nums.",".$MyDb->insertId;
echo "<br>";
$MyDb->test("DELETE FROM test2 WHERE name='Zeng Hua'");
echo $MyDb->affect_nums.",".$MyDb->insertId;
Sorry, lastInsertid() in the code is missing two brackets. I don’t know why it was missing when I copied it. The problem is not this...