2

I'm working on a webpage that will display the information of a user in my database. To begin with, I wrote some code to fetch the first name from the DB and have echoed it. The user I initially tested on has every field (Username, FirstName, LastName, Password) filled in as "1". To test it further I have tried logging out and trying with a different account, with the firstName "Admin". For this account and all accounts that aren't the first I am given this error:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /var/www/vhosts/s4001175-ct4009.uogs.co.uk/httpdocs/profile.php on line 19

I understand from looking at similar threads that this is likely because the query is returning False rather than an array as it should which lead me to believe that destroying the session wasn't working properly, however manually deleting the cookie, closing chrome still gives the same results.

Code:

<?php
// Starts the session 
session_start();
// If a user is not logged in, redirect to index to login
if(!isset($_SESSION['login_user'])){
   header("Location:index.html");
}


include 'config.php';

$currentUser = ($_SESSION['login_user']);


$selectUserInfo= "SELECT FirstName FROM User WHERE Username = $currentUser";

$userInfoResult = $connection -> query($selectUserInfo);

$row = mysqli_fetch_array($userInfoResult);
$FirstName = $row['FirstName'];
$LastName = $row['LastName'];
?>

Using the variable:

<?php echo "<h1>$FirstName $LastName</h1>"; ?>

As Suggested, I have implemented a While loop as below and modified the SQL to select all the info I need rather than just the FirstName:

<?php
// Starts the session 
session_start();
// If a user is not logged in, redirect to index to login
if(!isset($_SESSION['login_user'])){
   header("Location:index.html");
}

// Includes everything from config php to set up DB connection
include 'config.php';
// Creates var from current users username 
$currentUser = ($_SESSION['login_user']);

// Query to locate the first name using the existing login as a locator
$selectUserInfo= "SELECT * FROM User WHERE Username = $currentUser";
//Executes the query and creates a variable from it
$userInfoResult = $connection -> query($selectUserInfo);

//$row = mysqli_fetch_array($userInfoResult);

while ($row = mysqli_fetch_array($userInfoResult)) {
        $FirstName = $row['FirstName'];
        $LastName = $row['LastName'];
      }
?>

The same error still remains but went away for a minute while still not working. There seem to be some weird inconsistencies for whatever reason but the error I am getting is still:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /var/www/vhosts/s4001175-ct4009.uogs.co.uk/httpdocs/profile.php on line 21

1 Answer 1

2

You can try this:

<?php
session_start();
if(!isset($_SESSION['login_user'])){
   header("Location:index.html");
}
include 'config.php';
// Creates var from current users username 
$currentUser = ($_SESSION['login_user']);

$sql = "SELECT * FROM User WHERE Username = '$currentUser'";
$query = mysqli_query($connection, $sql);

    while ($row = mysqli_fetch_array($query)) {
            $FirstName = $row['FirstName'];
            $LastName = $row['LastName'];
          }
?>
5
  • 1
    Thanks, I've implemented this however I seem to get the same error still. Looking at it, the error pops up for a bit then disappears, however nothing is displayed now, not even an error.
    – SourOddity
    Feb 7, 2021 at 17:27
  • 1
    "You need to use a while statement as such:" Huh??
    – Alon Eitan
    Feb 7, 2021 at 17:27
  • 1
    Thanks for the edit, but unfortunately the same error is still being thrown.
    – SourOddity
    Feb 7, 2021 at 17:49
  • 1
    Tried again, same thing. Interestingly though, while my old method for some reason worked for 1 of the accounts, it not longer works for that either.
    – SourOddity
    Feb 7, 2021 at 17:54
  • 1
    Yeah, this error: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, bool given in /var/www/vhosts/s4001175-ct4009.uogs.co.uk/httpdocs/profile.php on line 24
    – SourOddity
    Feb 7, 2021 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.