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

Asked

Viewed 1,352 times

5

I know the expression mysql was discontinued, so I’m trying to use the expression mysqli, but every time I try to show myself on the screen a syntax error! example:

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

Generates this error:

Warning: mysqli_query() expects at least 2 Parameters

How do I correct that mistake?

2 answers

6

It is necessary to pass two requirements when the 'procedural mode' is used. The first is the connection and the second 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"); 

4

See the method signature on manual

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

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

In the 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");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.