php - Error mysqli_insert_id -


using the php manual created following code:

$query = "insert inserir(nome) values ('stefanato');"; $listar = new consultar(); $listar->executa($query);  echo "new record has id: " . mysqli_insert_id($listar->$query); 

i used answer class connections: error mysqli_select_db

but keep getting error:

warning: mysqli_insert_id () expects parameter 1, 2 given in /home/controle/public_html/demo/teste.php on line 9

how fix that?

here simple example getting id of last record created-

code taken here

<?php     $con=mysqli_connect("localhost","my_user","my_password","my_db");     // check connection     if (mysqli_connect_errno()) {         echo "failed connect mysql: " . mysqli_connect_error();     }     mysqli_query($con,"insert persons (firstname,lastname,age)         values ('glenn','quagmire',33)");      // print auto-generated id     echo "new record has id: " . mysqli_insert_id($con);      mysqli_close($con); ?>  

edit: here working code

<?php define("servidor_bd", "localhost"); define("usuario_bd", "usuario"); define("senha_bd", "senha"); define("banco_de_dados", "dados");   class conecta {     public $database_bancodados = null;//1. new added field     public $bancodados = null;//2. new added field     function conecta($servidor="", $bancodedados="", $usuario="", $senha=""){         if (($servidor == "") && ($usuario == "") && ($senha == "") && ($bancodedados == "")){             $this->bancodados = mysqli_connect(servidor_bd, usuario_bd, senha_bd) or trigger_error(mysqli_error(),e_user_error);//3. store in class variable             $this->database_bancodados = banco_de_dados;//4. store in class variable         } else {             $this->bancodados = mysqli_connect($servidor, $usuario, $senha) or trigger_error(mysqli_error(),e_user_error);//5. store in class variable             $this->database_bancodados = $bancodedados;//6. store in class variable         }     }     }  class consultar {      var $bd;     var $res;     var $row;     var $nrw;     var $data;      function executa($sql=""){         if($sql==""){             $this->res =  0; // pointer result of executed query             $this->nrw =  0; // line number query returned, cruise control             $this->row = -1; // array of current query line         }         // connects database               $this->bd = new conecta();//7. store in class variable            $this->bd->conecta();//8. store in class variable            mysqli_select_db($this->bd->bancodados, banco_de_dados);//9. change here parameter sequence             $this->res = mysqli_query($this->bd->bancodados, $sql); //10. change here parameter sequence             $this->nrw = @mysqli_num_rows($this->res);          $this->row = 0;         if($this->nrw > 0)             $this->dados();     }      function primeiro(){         $this->row = 0;         $this->dados();     }      function proximo(){         $this->row = ($this->row<($this->nrw - 1)) ?                         ++$this->row:($this->nrw - 1);         $this->dados();     }      function anterior(){         $this->row = ($this->row > 0) ? -- $this->row:0;         $this->dados();     }              function ultimo(){         $this->row = $this->nrw-1;         $this->dados();     }      function navega($linha){         if($linha>=0 , $linha<$this->nrw){             $this->row = $linha;             $this->dados();         }     }      function dados(){         mysqli_data_seek($this->res, $this->row);         $this->data = mysqli_fetch_array($this->res);     } }  $query = "insert inserir(uname) values ('stefanato');"; $listar = new consultar(); $listar->executa($query);  echo "new record has id: " . mysqli_insert_id($listar->bd->bancodados);//11. change here parameter 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -