Problem when trying to query with PHP and MYSQL

Asked

Viewed 46 times

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

1 answer

3


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

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

For the $this->conn must be the result of mysqli_connect, because it will contain the "link", the function mysqli_select_db It’s just to change banks, actually you don’t even need it, just if you want to change banks during the run, you can simplify to 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();
}
  • 1

    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!

  • 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?

  • 1

    @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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.