LastInsertId() returns a problem. I tested it and found that when deleting and modifying, lastInsertId() returned "0", so use if(null!== $this->xxx->lastInsertId()){} to judge. Can't distinguish between insertion, deletion and addition? Is there still something wrong with my test code? Can someone who knows please answer it? Thank you) [Using singleton mode]
黄粱一梦梦黄梁
黄粱一梦梦黄梁 2019-07-23 22:07:47
0
1
1171

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;

黄粱一梦梦黄梁
黄粱一梦梦黄梁

reply all(1)
黄粱一梦梦黄梁

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...

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template