Click here to Skip to main content
14,430,901 members
Rate this:
Please Sign up or sign in to vote.
See more:
I have an errors it says

Notice: Undefined variable: mysqli in C:\xampp\htdocs\cwang\users.php on line 40

Fatal error: Call to a member function query() on null in C:\xampp\htdocs\cwang\users.php on line 40

What I have tried:


$con = mysqli_connect("localhost","root","","dbmargs");

if (mysqli_connect_errno())
  echo "Failed to connect to MySQL: " . mysqli_connect_error();




					background-image: url('img/indexback.jpg');
					background-attachment: fixed;
					background-size: 100% 100%;

<title>MySQLi Read Records</title>
 <link rel="stylesheet" type="text/css" href="css/style.css">


<ul class="topnav">

    <li><a href="#home">Home</a></li>



include 'dbconnect.php';

$query = "select * from tblregister";

$result = $mysqli->query( $query );

$num_results = $result->num_rows;

echo "<div><a href='add.php'>Create New Record</a></div>";

if( $num_results > 0){ 

echo "<table border='1'>";

echo "<tr>";

echo "<th>Firstname</th>";

echo "<th>Lastname</th>";

echo "<th>Username</th>";

echo "<th>Action</th>";

echo "</tr>";

while( $row = $result->fetch_assoc() ){


echo "<tr>";

echo "<td>{$firstname}</td>";

echo "<td>{$lastname}</td>";

echo "<td>{$username}</td>";

echo "<td>";

//just preparing the edit link to edit the record

echo "<a href='edit.php?id={$id}'>Edit</a>";

echo " / ";

echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";

echo "</td>";

echo "</tr>";


echo "</table>";//end table


//if database table is empty

echo "No records found.";


//disconnect from database





Updated 9-Dec-19 2:21am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You are mixing the object oriented and the procedural style when executing mysqli functions. I suggest to choose only one style.

dbconnect.php uses the procedural style and assigns the object to $con.

In user.php you are calling the query function in the object oriented style using the undefined $mysqli object:
$result = $mysqli->query( $query );

You must use the object returned from connecting instead:
$result = $con->query( $query );
or use the procedural style:
$result = mysqli_query( $con, $query );

See also the examples at PHP: mysqli::query - Manual[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100