Cant login: mysqli_query() expects at least 2 para

2019-03-06 19:16发布

I have a login script that doesn't work anymore. It has something to do with the new mysqli. So I changed mysql query to mysqli but still get errors:

if (empty($errors)){
    $query = "SELECT id, username ";
    $query .= "FROM users ";
    $query .= "WHERE username = '{$username}' ";
    $query .= "AND hashed_password = '{$hashed_password}' ";
    $query .= "LIMIT 1";
    $result_set = mysqli_query($query);
    confirm_query($result_set);
    if (mysqli_num_rows($result_set) == 1){
        $found_user = mysqli_fetch_array($result_set);
        $_SESSION['user_id'] = $found_user['id'];
        $_SESSION['username'] = $found_user['username'];
        redirect_to("faculty.php");
    } else {
        $message = " Username / Password is incorrect.";
    }

That is my code, mysqli_query($query); is the problem. When I change it to mysqli_query($connection , $query);, I don't get the error anymore but I get the message:

Username / Password is incorrect.

When it is correct.

My connection script:

<?php
require("constants.php");

$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if (!$connection){
    die("Unable to connect to page");   
}

?>

<?php
$db = mysqli_select_db($connection , "fm_cms");
if (!$db){
    die("Unable to connect to Database");   
}
?>

<?php
$db = mysqli_select_db($connection , "fm_cms");
if (!$db){
    die("Unable to connect to Database");   
}
?>

1条回答
一夜七次
2楼-- · 2019-03-06 19:52

MySQLI requires both the connection and the query in the parameters.

mysqli_query($con,$query);

Full practical:

$Con = mysqli_connect(/* Connection Information here*/);
$Result_Set = mysqli_query($Con,"SELECT ID FROM table");

In response to your updated question body, the mysqli_select_db is to be used when one wishes to change the current working schema (database). In this case, originally connecting to the schema specified in the constant DB_NAME. If you are not wanting to swap schemas but keep the same. Then remove the mysqli_select_db

and a working MySQLi Query construct would be:

$connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
$Query = mysqli_query($connection,"QUERY HERE");
if (!$connection){
    die("Unable to connect to page");   
}

and in response to; i dont get the error anymore but i get the message: Username / Password is incorrect. when it is correct.. Help please!

You are being presented this message because you have told PHP to do so, as your mysqli_num_rows is returning false (due to a incorrect mysqli_query being supplied) the numbered rows will not be equal to 1, but be interpreted as 0 (Possible Refence)

MySQLi Query Documentation

查看更多
登录 后发表回答