Always present an error when I try to use the mysqli expression

5

I know that the expression mysql has been deprecated, so I'm trying to use the expression mysqli , but every time I try it shows me a syntax error! example:

$buscaDados = mysqli_query("SELECT * FROM usuario"); 

Generate this error:

  

Warning: mysqli_query () expects at least 2 parameters

How do I fix this error?

    
asked by anonymous 04.03.2014 / 23:13

2 answers

6

It is necessary to pass two agumentos when the 'procedural mode' is used. The first is the connection and the second is the query.

mysqli_query

  

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

Your code should look like this:

$buscaDados = mysqli_query($conexao, "SELECT * FROM usuario"); 
    
04.03.2014 / 23:18
4

View the signature of the method in the manual

  

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

This means that you need to pass the connection (which is a resource value) as the first parameter, and query as the second.

In object-oriented version it is possible to use a parameter only:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$buscaDados = $mysqli->query("SELECT * FROM usuario");
    
04.03.2014 / 23:19