lixle
lixle

Reputation: 111

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in

I got the warning:

Warning: mysqli_query() expects parameter 1 to be mysqli, string given in (...) on line 6

My code is here:

<?php

require_once 'conn.php';

$sql = "SELECT user_id, access_lvl, name FROM cms_users ";
$result = mysqli_query($sql, $conn);

Upvotes: 7

Views: 76903

Answers (4)

Ray
Ray

Reputation: 41428

It's exactly as the error states as you're passing arguments to mysqli_query() incorrectly. Assuming $conn is your mysqli connection generated at some point by new mysqli() it should be:

$result = mysqli_query( $conn,$sql) or trigger_error(mysqli_error($conn)));

The way you were calling it you were passing a string, $sql as the first argument.

Upvotes: 19

AdheneManx
AdheneManx

Reputation: 348

I have the same error, although the $result = mysqli_query($conn, $sql) is the correct way around.

I var_dump() the $conn object and it is a set object at the time I run the query, but still returns a 'string given' error.

I was accessing the $conn object after being parsed into a function that I was using it with, in the same way I've done throughout the whole project without error.

Re-declaring the $conn object inside the function, instead of passing it into the function stopped the errors, although this behaviour doesn't occur anywhere else in my project. This isn't an ideal solution either.

To note: I'm using a .env for local development, which causes no issues and helps with deployment locally/remotely via .git.

After many hours, I honestly believe there is a PHP bug here, I'm using 7.3.0, but occurred in 7.2.5 as well as I'm definitely parsing it a db connection object, not a string.

Just posting this for information purposes, in case anyone else runs into it. Thanks.

PS. Passwords shouldn't be stored in the database in plain text and is a major security concern. If the author hasn't adjusted this yet (I know it's an old post), it's important to read:

Secure hash and salt for PHP passwords

Upvotes: -1

Zakhele Harries
Zakhele Harries

Reputation: 41

I was having this this problem but after swiping $result = mysqli_query($sql, $conn) to $result = mysqli_query( $conn,$sql)

I manage to solve this error

Upvotes: 3

Sahil
Sahil

Reputation: 1413

I am 3 years too late but all you need to do is swap the parameters of mysqli_query()

$result = mysqli_query($conn, $sql)

Upvotes: 1

Related Questions