Sql query keeps returning bool(false)

function __construct() {
    $this->conn = mysqli_connect($this->host, $this->username, $this->password);
}

public function query(){
    if (mysqli_connect_errno()) return "Failed to connect to MySQL: " . mysqli_connect_error();

    $sql = "SELECT * FROM users";
    $result = mysqli_query($this->conn, $sql);
    return $result;
}

I was quite tired last night so I forgot to give some extra information.
I have this function inside a class “Database” and I want to execute this function “query()” from another page:

<?php echo($database->query()) ?>

But this always returns bool(false) instead of users although my connection is okay.
So if anyone could help it would be very much appreciated!

By “the SQL query”, do you mean that the mysqli_query function returns false? If so, that sounds like your query is generating an error.

Can you please check if the function mysqli_error($this->conn) returns any information after running mysqli_query? That should contain an error message with more information about why the query failed.

1 Like

Apparantly there was no database selected.
But after adding this line: $this->conn->select_db($this->database);
$result returns: object(mysqli_result)#3 (5) { ["current_field"]=> int(0) ["field_count"]=> int(4) ["lengths"]=> NULL ["num_rows"]=> int(1) ["type"]=> int(0) }
I’m not really good with databases so I don’t really know what this means…

The object(mysqli_result) means the query was successful and returned a result. Please check the PHP documentation or a good MySQL tutorial to learn how to use the results.

Thanks for the support!
I know what to do now.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.