0

I have a mysql query which doesn't work. It gives me the following error:

mysqli_query() expects parameter 1 to be mysqli, null given

my sql query is:

$select = mysqli_query($sql, "SELECT title FROM category WHERE id LIKE (SELECT categorie_id FROM categories_sub WHERE file LIKE '".$site."')")
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
asd
  • 138
  • 9
  • 2
    `$sql` has to be an instance of MySQLi. Are you instantiating it? – Jeremy Harris May 28 '14 at 18:09
  • 1
    `$sql` <= if that isn't your DB connection variable, then that could be an issue. – Funk Forty Niner May 28 '14 at 18:11
  • this is my database connection variable. – asd May 28 '14 at 18:20
  • So, what does your DB connection code look like? That error doesn't pop up for nothing. Are you using it from a class? Plus, from what I saw in your other questions, all seem to be OK, up to this. – Funk Forty Niner May 28 '14 at 18:39
  • ok it works. the reason for the error was, i have mistake use a incorrect database name. – asd May 28 '14 at 18:56
  • Great, glad to hear it. Always use as much error reporting codes as possible when in development. That would have signaled the error right way. Some just use `die("There was an error.");` instead of check for real errors such as `error_reporting(E_ALL); ini_set('display_errors', 1); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` or `mysqli_error()` or `mysql_error()` depending on the API used. – Funk Forty Niner May 28 '14 at 19:35

2 Answers2

1

The query will not work, what your query is doing

WHERE id LIKE (SELECT categorie_id FROM categories_sub WHERE file = 'some val')

This is similar as

WHERE id = {multiple categorie_id} 

when the subquery has more than one categorie_id and this will return error.

So replace

category WHERE id LIKE 

to

category WHERE id IN ( ...
Abhik Chakraborty
  • 44,654
  • 6
  • 52
  • 63
  • my query looks like `"SELECT title FROM category WHERE id IN (SELECT categorie_id FROM categories_sub WHERE file LIKE '".$site."')"` but it still not working. The Error is the same .. – asd May 28 '14 at 18:20
0

This error tends to mean that you didn't connect correctly to the database. Double check your connection. Also, file LIKE 'x' is equivalent to file='x'. To search for a string containing 'x', use file LIKE '%x%'.

Edit: The other answers are right that you need to use IN instead of LIKE for the first one (i.e. a member of the set defined on another query)

Scelesto
  • 775
  • 6
  • 13