Vuyisile Vuyisile - 1 year ago 129
SQL Question

mysqli_fetch_query returns boolean

Good day, Im trying read all records from my table but I keep getting this error

message: 'Warning: mysql_fetch_array() expects parameter 1 to be
mysqli_result, boolean given in

here is my code :

$conn = mysqli_connect("localhost", "root", "") or die (mysql_error()); //Connect to server
$username = mysqli_real_escape_string($conn,$_POST['username']);
$password = mysqli_real_escape_string($conn,$_POST['password']);
$bool = true;

//mysql_connect("localhost", "root", "") or die(mysql_error());// connects to the server
mysqli_select_db($conn,"first_db") or die("Cannot connect to the database");//connects to the db
$query = ("select * from user");//
$result = mysqli_query($conn,$query);//query the user table

while($row = mysqli_fetch_array($result))//display all rows from the query
$table_users = $row['username'];//the first username row is passed on to $table_users,and so on until the query is finished
if($username == $table_users)//checks if there are any mixing fields
$bool = false;//sets bool to false
Print '<script>alert("Username has been taken!") </script>';//prompts the user
Print '<script>window.location.assign("register.php")</script>';//redirects user to register.php


if($bool)//sets bool to true
mysqli_query($conn,"INSERT INTO user (username,password) VALUES ('$username','$password')");//inserts the value to table users
Print '<script>alert("Successfully Registered!") </script>';//prompts the user
Print '<script>window.location.assign("register.php")</script>';//redirects user to register.php

echo "Username entered is: ". $username. "<br/>";
echo "Password entered is: ". $password;

Answer Source

This is because you mixing mysql and mysqli.

This is problem with database connection

You have to write connection like this

$conn =  mysqli_connect("localhost", "root", "","first_db"); 

Instead of this:

$conn =  mysqli_connect("localhost", "root", ""); 
 mysqli_select_db($conn,"first_db") or die("Cannot connect to the database");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download