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");
}
?>
MySQLI requires both the connection and the query in the parameters.
Full practical:
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 constantDB_NAME
. If you are not wanting to swap schemas but keep the same. Then remove themysqli_select_db
and a working MySQLi Query construct would be:
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 incorrectmysqli_query
being supplied) the numbered rows will not be equal to 1, but be interpreted as 0 (Possible Refence)MySQLi Query Documentation