Monu Joshi Monu Joshi - 2 years ago 179
MySQL Question

php mysqli query expects parameter 1 be to string

I was coding a User database which calls an static method of find_all which calls an query method which in turn returns all the value of a database but i am finding trouble in doing so each time when i try to call it it gives me these error as mysqli_query() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\photogallery\includes\database.php on line 20.What do i do? Here is the code snippet

class User{

public static function find_all(){
global $database;
$sql= "select * from users";
$result_set = $database->query($sql);

return $result_set;
public static function find_by_id($id=0){
global $database;
$result_set = $database->query("SELECT FROM users where id={$id}");
$found = $database->fetch_array($result_set);
return $found;


public function query($sql){
echo $sql;
$this->last_query = $sql;
echo $result = mysqli_query($sql,$this->connection);
return $result;


public function confirm_query($result){
die("Database query failed:".mysqli_error());

and the index.php where the code is called as follows :


$q = User::find_all();


Each time the error comes as mysqli_query expects parameter 1 to be mysqli,string given in C:\xampp\htdocs\photogallery\includes\database.php on line 20.Help me out please??

Answer Source

When using mysqli in a procedural style, the first argument to mysqli_query must be a connection object

From the official documentation:

mixed mysqli_query ( mysqli $link , string $query [, int $resultmode = MYSQLI_STORE_RESULT ] )

So you need to change the order of the arguments, $this->connection goes first and $sql goes second:

$result = mysqli_query($this->connection, $sql);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download