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);
}
}