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?
 
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
 
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
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.