user3251639 user3251639 - 11 days ago 8
PHP Question

ERROR: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

getting this error in login, I have installed new version of xampp/php still facing lots of problems for mysqli...

below is code for login.php after that it goes to lock.php the code is below...

<?php

include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from form

$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);


$sql="SELECT id FROM mc_admin WHERE username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];

$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;

header("location: home.php");
}
else
{
$error="Your Login Name or Password is invalid";
}
}
?>


code for lock.php below:

<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];

$ses_sql=mysql_query("select username from mc_admin where username='$user_check' ");

$row=mysql_fetch_array($ses_sql);

$login_session=$row['username'];

if(!isset($login_session))
{
header("Location: login.php");
}
?>

Answer

MYSQL query returns FALSE on error, So one of your queries has an error while executing.

As per PHP docs mysql_query() returns a resource on success, or FALSE on error So before trying to call the mysql_fetch_array check as below:

You can print the error

$result = mysql_query('your query');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

Also you must note that now mysql_* functions are deprecated as of PHP5.5. INstead of mysql_* you could use mysqli or PDO