Problem when trying to query with PHP and MYSQL

1

I'm trying to do a query directly in the PHP file where it creates the connection to the database. This query will create my table. The problem is that I am trying to reference the connection variable in mysqli_query () and it is not being referenced (I believe this is the problem). I made this structure because I'm doing Object Oriented!

The error you are giving is: Warning: mysqli_query () expects parameter 1 to be mysqli, boolean given in ...

Follow the code:

<?php

class db{

    private $dbServer = "localhost";
    private $dbUser = "root";
    private $dbPassword = "";
    private $dbName = "sql_queries";
    private $conn;

    public function __construct(){
        $this->conn = mysqli_connect($this->dbServer, $this->dbUser, $this->dbPassword) or die("Não foi possível estabelecer a conexão com a base de dados! ".mysqli_error($this->conn));
        $this->conn = mysqli_select_db($this->conn, $this->dbName) or die("Não foi possível se conectar com o banco de dados! ".mysqli_error($this->conn));

        $this->setQuery();
    }

    public function getConn(){
        return $this->conn;
    }

    public function setQuery(){

        $sql = "CREATE TABLE IF NOT EXISTS sql_queries.aluno(
            aluno_id INT NOT NULL AUTO_INCREMENT,
            nome_aluno varchar(255),
            dataNascimento_aluno date,
            nome_pai varchar(255),
            nome_mae varchar(255),
            RG_aluno char(10),
            cpf_aluno char(11),
            telefone_aluno char(10),
            celular_aluno char(13),
            CONSTRAINT PK_aluno PRIMARY KEY (aluno_id)
            );";

            $result = mysqli_query($this->conn, $sql);
    }
}
    
asked by anonymous 04.01.2018 / 19:52

1 answer

3

The problem is that you set the $this->conn twice:

$this->conn = mysqli_connect($this->dbServer, $this->dbUser, $this->dbPassword) or die("Não foi possível estabelecer a conexão com a base de dados! ".mysqli_error($this->conn));
$this->conn = mysqli_select_db($this->conn, $this->dbName) or die("Não foi possível se conectar com o banco de dados! ".mysqli_error($this->conn));

Should be:

$this->conn = mysqli_connect($this->dbServer, $this->dbUser, $this->dbPassword) or die("Não foi possível estabelecer a conexão com a base de dados! ".mysqli_error($this->conn));
mysqli_select_db($this->conn, $this->dbName) or die("Não foi possível se conectar com o banco de dados! ".mysqli_error($this->conn));

Because $this->conn should be the result of mysqli_connect , since it will contain " link ", the mysqli_select_db function is just to change banks, you do not actually need it, only if you want to change during execution, you can simplify this:

public function __construct(){
    $this->conn = mysqli_connect($this->dbServer, $this->dbUser, $this->dbPassword, $this->dbName) or die("Não foi possível estabelecer a conexão com a base de dados! ".mysqli_error($this->conn));

    $this->setQuery();
}
    
04.01.2018 / 20:00