Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\test\personal details functions.php on line 71 hi i keep getting this error and i cant solve it

<?php
    session_start();
// connect to database
$db = mysqli_connect('localhost', 'root', '12345', 'personal details');

// variable declaration
$username = "";
$Email    = "";
$Phonenumber = "";
$country = "";
$errors = array();

// call the register() function if register_btn is clicked
if (isset($_POST['register_btn'])) {
      register();
}

// REGISTER USER
function register(){
      // call these variables with the global keyword to make them available in function
      global $db, $errors, $username, $Email , $Phonenumber , $country;

      // receive all input values from the form. Call the e() function
    // defined below to escape form values
      $username    = e($_POST['username']);
      $Email       = e($_POST['Email']);
      $Phonenumber = e($_POST['Phonenumber']);
    $country     = e($_POST['country']);
      // form validation: ensure that the form is correctly filled
      if (empty($username)) {
            array_push($errors, "username is required");
      }
      if (empty($Email)) {
            array_push($errors, "Email is required");
      }
      if (empty($Phonenumber)) {
            array_push($errors, "Phonenumber is required");
    }
      if (empty($country)) {
            array_push($errors, "country is required");
    }
      // register user if there are no errors in the form
      

            if (isset($_POST['personal details'])) {
                  $user_type = e($_POST['personal details']);
                  $query = "INSERT INTO users (username, Email, Phonenumber, country )
                                VALUES('$username', '$Email', '$Phonenumber', '$country' )";
                  mysqli_query($db, $query);
                  $_SESSION['success']  = "New user successfully created!!";
                  header('C:\wamp64\www\test\HOME PAGE 1.HTML');
            }else{
                  $query = "INSERT INTO personal details (username, Email, Phonenumber, country )
                                VALUES('$username', '$Email', '$Phonenumber', '$country' )";
                  mysqli_query($db, $query);

                  // get id of the created user
                  $logged_in_personaldetails_id= mysqli_insert_id($db);

                  $_SESSION['personaldetails'] = getUserById($username); // put logged in user in session
                  $_SESSION['success']  = "New user successfully created!!";
                  header('HOME PAGE 1.HTML');
            }
      }
// return user array from their id
function getUserById($username){
      global $db;
      $query = "SELECT * FROM `personal details` WHERE username=" . $username;
      $result = mysqli_query($db, $query);

      $personaldetails = mysqli_fetch_assoc($result);
      return $username;
}

// escape string
function e($val){
      global $db;
      return mysqli_real_escape_string($db, trim($val));
}

function display_error() {
      global $errors;

      if (count($errors) > 0){
            echo '<div class="error">';
                  foreach ($errors as $error){
                        echo $error .'<br>';
                  }
            echo '</div>';
      }
}

function isLoggedIn()
{
      if (isset($_SESSION['user'])) {
            return true;
      }else{
            return false;
      }
}
?>
mark louieAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Elangovan SundarSenior Software EngineerCommented:
mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given
This means that the first parameter you passed is a boolean (true or false).

@marklouie

The first parameter is $result, and it is false because there is a syntax error in the query.

Note : You wrongly mentioned your table name

$query = "INSERT INTO personal details (username, Email, Phonenumber, country ) 
VALUES('$username', '$Email', '$Phonenumber', '$country' )";

Open in new window


Replace below code

$query = "INSERT INTO users (username, Email, Phonenumber, country ) 
VALUES('$username', '$Email', '$Phonenumber', '$country' )";

Open in new window


Hope it will works as you expected. Thanks..!
1
Nitin SontakkeDeveloperCommented:
I have virtually no knowledge of php, but get hugely uncomfortable when I see spaces used in object names, etc.

For example, see below:

$query = "SELECT * FROM `personal details` WHERE username=" . $username;

Open in new window


And compare that with this one...

$query = "INSERT INTO personal details (username, Email, Phonenumber, country ) 

Open in new window


What happened to single quotes around table name?

Furthermore,

Why not construct the query as follows, which is lot safer, isn't it?

$query = "SELECT * FROM `personal details` WHERE username = '$username'";

Open in new window


As similar technique used here....

 VALUES('$username', '$Email', '$Phonenumber', '$country' )";

Open in new window


I am aware that I am not quite answering your question, but be very careful while coding as you would get unexpected result out of such coding practises.
1
Vijaya KumarCommented:
try this:
check your table name is personal details
the above your query return false (0) that's why it shows boolean given instead of string
$query = "SELECT * FROM `personal details` WHERE username=' $username' ";

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

mohan singhWeb developerCommented:
Don't do like this
$query = "SELECT * FROM `personal details` WHERE username=" .$username;

Open in new window


Do like this

$query = "SELECT * FROM personal details WHERE username='$username'";

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vijaya KumarCommented:
if u have any my sql tools like workbench or phpmyadmin then run the query...

also personal details considered asMysql_error.png personal. rename table like personal_details
0
mohan singhWeb developerCommented:
write query like this

select * from [personnel details]
1
Vijaya KumarCommented:
sorry now try this
   try {
      $query = "SELECT * FROM `personal details` WHERE username=' $username' ";
      $result = mysql_query($query) or die(mysql_error());
   } catch (mysqli_sql_exception $e) {
      print_r ($e);
   }
0
mark louieAuthor Commented:
thank you instructors for helping me on my project i'v been stuck to this code for an hour and i'm still learning some of these codes from a book and a few videos on the internet, because i'm still a college student maybe next time i'll try to help you guys in a different field in IT course.
2
Vijaya KumarCommented:
@mark louie it works or not,

we here for to help u and others, any time welcome...
0
mark louieAuthor Commented:
it works thank you
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.