当我成功插入时,mysql_insert_id()返回0

时间:2015-03-16 23:36:13

标签: php mysql

我有这个课程给我的DB。

当我执行一个插入(成功的)调用函数 singleQuery 时,$ id = mysql_insert_id();给我回报0.为什么?

我想获取auto_increment id。

我做错了什么?

谢谢!

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 IS HERE
                $cuenta = $sentencia->rowCount();

                echo $id; //PRINTS a 0 :(
            }
            $this->disconnect();
        }else{
            $cuenta=-1;
        }
        return $cuenta;
    }

0 个答案:

没有答案