1
I’m trying to make 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’s not being referenced (I believe this is the problem). I did it in this structure because I’m doing Object-Oriented!
The mistake you’re making is: Warning: mysqli_query() expects Parameter 1 to be mysqli, Boolean Given in...
Follows 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);
    }
}
						
Perfect! After doing what you said it worked!!! I didn’t know that I didn’t need to reference twice, and that the function mysqli_select_db() is only for switching banks! I’ll keep the tip in mind!
– Wesley Redfield
William, actually, in this line of code $this->Conn = mysqli_select_db I’m overwriting the "Link" with another result... so it didn’t work right?
– Wesley Redfield
@Wesleyredfield that’s right, mysqli_select_db does not contain the link, it only returns true or false, to say whether you were able to change the database or not.
– Guilherme Nascimento