Error mysqli_select_db [closed]

2019-06-14 16:43发布

问题:

Hello everybody where I went wrong in this connection?

Warning: mysqli_select_db() expects exactly 2 parameters, 1 given in /home/controle/public_html/trabd01/classesBD.php on line 31

define("SERVIDOR_BD", "localhost");
define("USUARIO_BD", "usuario");
define("SENHA_BD", "senha");
define("BANCO_DE_DADOS", "dados");


class conecta {
        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);
                $this->database_bancoDados = BANCO_DE_DADOS;
            } else {
                $this->bancoDados = mysqli_connect($servidor, $usuario, $senha) or trigger_error(mysqli_error(),E_USER_ERROR);
                $this->database_bancoDados = $bancoDeDados;
            }
        }    
    }

    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   
               $bd = new conecta();
               $bd->conecta();            
               mysqli_select_db(BANCO_DE_DADOS, $bd->bancoDados);/*ERRORHERE*/
               $this->res = mysqli_query($sql, $bd->bancoDados);
               $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);
        }
    }

回答1:

In mysqli_ the connection comes first.

You have:

mysqli_select_db(BANCO_DE_DADOS, $bd->bancoDados);/*ERRORHERE*/
$this->res = mysqli_query($sql, $bd->bancoDados);

when it should read as, and to inverse those:

mysqli_select_db($bd->bancoDados, BANCO_DE_DADOS);
$this->res = mysqli_query($bd->bancoDados, $sql);

References:

  • http://php.net/manual/en/mysqli.select-db.php
  • http://php.net/manual/en/mysqli.query.php

Plus, check for errors on your query also:

  • http://php.net/manual/en/mysqli.error.php

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.


Nota:

The @ symbol in @mysqli_num_rows is an error suppressor. You may want to remove it during testing.



标签: php mysqli