Connection class mysqli in php and SELECT [duplicate]

0

The connection code with the database that I have mounted is working, but when I use a SELECT, an error appears and I could not resolve it.

<?php
define('DB_NAME', 'projetoxyz');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_HOST', 'localhost');

class conexao extends mysqli {

    public $servidor;
    public $usuario;
    public $senha;
    public $banco;

    function __construct($servidor, $usuario, $senha, $banco) {
        $this->servidor = $servidor;
        $this->usuario = $usuario;
        $this->senha = $senha;
        $this->banco = $banco;
    }

    public function conectar()
    {
        try
        {
            parent::__construct($this->servidor, $this->usuario, $this->senha, $this->banco);
            if(mysqli_connect_errno() != 0)
            {
                throw new Exception('Erro ao conectar!');
            }
        }
        catch(Exception $e)
        {
            echo $e->getMessage();
        }
    }
}

$mysqli = new conexao(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

#################################################################

if( !function_exists('nomecidade') ) {

    function nomecidade($cidade=NULL) {

        if(!isset($mysqli)) {
          $mysqli = new conexao(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
        }

        $mysqli->conectar();

        $cidade = trim($cidade);

        $cidade = $mysqli->real_escape_string($cidade);

        /* Em WHERE, o campo Código é INTEGER, o SELECT pega o código da Cidade e retorna o nome da cidade relacionada ao código*/

        $sqlCidade = "SELECT 
                            'Nome' 
                        FROM 
                            'municipio' 
                        WHERE 
                            'Codigo' = ? 
                        LIMIT 
                            1
                        ;";

        $stmt = $mysqli->prepare($sqlCidade);

        $stmt->bind_param("i", $cidade);

        $stmt->execute();

        $result = $stmt->get_result();

        if($result->num_rows === 0) exit('Cidade indefinida');

        $row = $result->fetch_assoc();

        $cidade = stripslashes( $row['Nome'] );

        $stmt->close();

        return $cidade;

    }
}

echo nomecidade('1100056');
?>

The error on the screen is as follows:

Fatal error: Uncaught Error: Calling a member function bind_param () on boolean in F: \ xampp \ htdocs \ projectxyz \ conn \ conn.php: 67

Stack trace: # 0 F: \ xampp \ htdocs \ projectxyz \ index.php (331): name ('1100056') # 1 {main} thrown in F: \ xampp \ htdocs \ projectxyz \ index.php on line 67

    
asked by anonymous 31.05.2018 / 17:01

1 answer

1

I do not think you created statment , so just use prepare . Because calling the prepare function will return a boolean (which results in the error you received, because a Boolean value will not have the bind_param property). Do something like:

//......
$stmt =  $mysqli->stmt_init();
$stmt->prepare($sqlCidade);

$stmt->bind_param("i", $cidade);

$stmt->execute();
//.......
    
31.05.2018 / 17:29