dpub33855 2016-01-27 16:19
浏览 66
已采纳

mysqli_insert_id错误

Using the PHP manual I created following code:

$query = "INSERT INTO inserir(nome) VALUES ('Stefanato');";
$listar = new consultar();
$listar->executa($query);

echo "New record has id: " . mysqli_insert_id($listar->$query);

I also used this answer for class connections: Error mysqli_select_db

But I keep getting this error:

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

How do I fix that?

  • 写回答

1条回答 默认 最新

  • duanhuan3012 2016-01-27 16:59
    关注

    Here is the simple example for getting the id of last record created-

    Code is taken from here

    <?php
        $con=mysqli_connect("localhost","my_user","my_password","my_db");
        // Check connection
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        mysqli_query($con,"INSERT INTO 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 is 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 the executed query
                $this->nrw =  0; // Line number the query returned, cruise control
                $this->row = -1; // Array of the current query line
            }
            // Connects to the 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 For parameter sequence
                $this->res = mysqli_query($this->bd->bancoDados, $sql); //10. Change here for 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 AND $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 INTO inserir(uname) VALUES ('Stefanato');";
    $listar = new consultar();
    $listar->executa($query);
    
    echo "New record has id: " . mysqli_insert_id($listar->bd->bancoDados);//11. Change Here for parameter
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能