7

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);
Dharman
  • 30,962
  • 25
  • 85
  • 135
lixle
  • 111
  • 1
  • 1
  • 3

4 Answers4

18

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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ray
  • 40,256
  • 21
  • 101
  • 138
  • @MarcB I wasn't a downvoter, but Ray originally had it as `mysql_error()` which won't show up in the edits. – Funk Forty Niner Oct 29 '14 at 20:10
  • @Fred-ii- Hasty cut-and-paste not seeing the far right of the die message. I removed it. – Ray Oct 29 '14 at 20:11
  • @MarcB Yeah, that's why they won't show up, due to the 5 minute allowance which Ray was still within that timeframe. – Funk Forty Niner Oct 29 '14 at 20:11
  • 1
    @Ray OP's mixing APIs `or die('Could not look up user information; ' . mysql_error());` which should read as `or die('Could not look up user information; ' . mysqli_error($conn));`. I never downvote hastily, btw. ;) – Funk Forty Niner Oct 29 '14 at 20:12
  • @Fred-ii- While not good form, and I'm not recommending this, but as long as there's only a single mysqli connection, the mysql commands should work fine. – Ray Oct 29 '14 at 20:14
  • 1
    @Fred-ii- I'll steal your comment and update the answer. – Ray Oct 29 '14 at 20:14
1

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

Nirav Joshi
  • 1,713
  • 15
  • 28
0

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

$result = mysqli_query($conn, $sql)
Sahil
  • 1,387
  • 14
  • 41
-1

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

AdheneManx
  • 306
  • 3
  • 11