php - When I do a successful insert, mysql_insert_id() returns 0 -
i have class db.
when insert (a success one) calling function singlequery, $id=mysql_insert_id(); returns me 0. why?
i want auto_increment id.
what doing wrong?
thanks!
class database{ //atributos private $connectionstring="mysql:host=localhost;dbname=practica"; private $user="root"; //usuario de la bbdd con permisos filtrados private $password="root"; private $db; private function connect(){ $flag=true; try{ $this->db = new pdo($this->connectionstring, $this->user, $this->password); }catch(pdoexception $e) { $flag=false; } return $flag; } private function disconnect(){ $this->db=null; } public function singlequery($sql,$vector){ //insert, update, delete if($this->connect()){ $fla="products"; $posicion = strpos($sql,$fla); if ($posicion === false) //normal { //code } else //especial x productes x obtenir la id del prod. insertat { $sentencia = $this->db->prepare($sql); $sentencia->execute($vector); $id=mysql_insert_id(); //the problem here $cuenta = $sentencia->rowcount(); echo $id; //prints 0 :( } $this->disconnect(); }else{ $cuenta=-1; } return $cuenta; }
Comments
Post a Comment