0

I have here a function which calls details from the database after log in. The problem I have is that I receive this:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in ...

Would you please point out to me what needs to be changed and how so that I would be able to get the data i needed from the database?

function profile(){
include "config.php";
$conn = mysqli_connect($host,$user,$pass,$db);

//check
if( mysqli_connect_errno($conn) ){
    echo "Error in DB";
}else{
    //connected
}

//prepare
$sql = "SELECT * FROM accounts where username='" . $_SESSION['username'] . "'";

//display 
$result = mysqli_query($conn,$sql);
$myrow=mysqli_fetch_array($result) ){ // --->the warning is from this line
        do{
            $info= array();
            $info['firstName'] = $myrow['firstName'];
            $info['middleInitial'] = $myrow['middleInitial'];
            $info['lastName'] = $myrow['lastName'];
            $info['mobile_no'] = $myrow['mobile_no'];
            $info['email_address'] = $myrow['email_address'];
            $info['registration_date'] = $myrow['registration_date'];
            $account[] = $info;
        }while($myrow=mysqli_fetch_array($result));
    }
    return $account;

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
D. Gatch
  • 167
  • 4
  • 13

3 Answers3

2

Your $result is null.

From PHP documentation mysqli_query:

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

hsz
  • 148,279
  • 62
  • 259
  • 315
0
USE

if($result){
    $myrow=mysqli_fetch_array($result) ){ 
            do{
                $info= array();
                $info['firstName'] = $myrow['firstName'];
                $info['middleInitial'] = $myrow['middleInitial'];
                $info['lastName'] = $myrow['lastName'];
                $info['mobile_no'] = $myrow['mobile_no'];
                $info['email_address'] = $myrow['email_address'];
                $info['registration_date'] = $myrow['registration_date'];
                $account[] = $info;
            }while($myrow=mysqli_fetch_array($result));
        }
        return $account;

    }
safin chacko
  • 1,345
  • 1
  • 11
  • 18
0

The mysqli_affected_rows() function returns the number of affected rows in the previous SELECT, INSERT, UPDATE, REPLACE, or DELETE query.

    <?php
    //display 
    $result = mysqli_query($conn,$sql);

    $count=mysqli_affected_rows($con);


     if($count>0)
     {
            $myrow=mysqli_fetch_array($result) ){ 
                    do{
                        $info= array();
                        $info['firstName'] = $myrow['firstName'];
                        $info['middleInitial'] = $myrow['middleInitial'];
                        $info['lastName'] = $myrow['lastName'];
                        $info['mobile_no'] = $myrow['mobile_no'];
                        $info['email_address'] = $myrow['email_address'];
                        $info['registration_date'] = $myrow['registration_date'];
                        $account[] = $info;
                    }while($myrow=mysqli_fetch_array($result));
                }
                return $account;

    }
    else
    {
       echo "empty";    
    }

    ?>
JYoThI
  • 11,977
  • 1
  • 11
  • 26