Reputation: 284
In the below code when I pass $id_num
to check the id field in database it accepts but when I want to pass user id to check with database it shows the following error;
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in line no 12
Can anyone tell me where I'm going wrong.
code:
if(isset($_POST['user_mail']) && isset($_POST['user_pass']))
{
$var_1=$_POST["user_mail"];
$var_2=$_POST["user_pass"];
$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail=$var_1");
while($row = mysqli_fetch_array($result))
{
if(($row['user_mail']==$var_1) && ($row['user_pass']==$var_2))//compare user name and password with database value
echo "Welcome";
else
echo "Try Again";
}
Upvotes: 0
Views: 61
Reputation: 11483
Use Prepared Statements for cleaning up your code:
$result = false;
$stmt = $con->prepare("SELECT * FROM jsrao_db2 WHERE user_mail=?");
$stmt->bind_result($result);
$result = $stmt->bind_param("s", $var_1)->execute();
if ($result) {
//work with $result
}
Upvotes: 1
Reputation: 3329
change your query
$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail=$var_1");<br>
should be
$result = mysqli_query($con,"SELECT * FROM jsrao_db2 WHERE user_mail='$var_1'");<br>
user_mail is an string so enclose $var_1 in '$var_1'
Upvotes: 2