user3744076
user3744076

Reputation: 127

Undefined variable: mysqli & call to a member function query() on null

I'm kinda new to mysqli and I'm trying to display data on a table.

Here's the code:

<tbody>
   <?php
   $query = $mysqli->query("SELECT username,password,FName,LName,userAddress FROM tbl_users");
   $no = 1;
   while($row = $query->fetch_assoc()){
   ?>
   <tr>
  <td><?php echo $no++ ?></td>
  <td><?php echo $row['username'] ?></td>
  <td><?php echo $row['password'] ?></td>
  <td><?php echo $row['FName'] .' '. $row['LName'] ?></td>
  <td><?php echo $row['userAddress'] ?></td>
  <td>
      <a href="update.php?id=<?php echo $row['userid'] ?>" class="btn btn-warning btn-sm"><span class="glyphicon glyphicon-pencil" aria-hidden="true"></span></a>
     <a onclick="return confirm('Are you sure you want to delete data')" href="delete.php?id=<?php echo $row['userid'] ?>" class="btn btn-danger btn-sm"><span class="glyphicon glyphicon-trash" aria-hidden="true"></span></a>
  </td>
  </tr>
  <?php
  }
  ?>
</tbody>

These are the errors:

Undefined variable: mysqli in C:\xampp\htdocs\company\admin\user_table.php on line 205

Fatal error: Call to a member function query() on null in C:\xampp\htdocs\company\admin\user_table.php on line 205

Upvotes: 1

Views: 6355

Answers (2)

Parth Chavda
Parth Chavda

Reputation: 1829

Undefined variable: mysqli means you didn't create object 'mysqli' or not able to get object mysqli; i.e. you will have to establish a connection to the MySQL server by creating an instance of mysqli before you can use this instance to send queries to the server.

create object

$mysqli = new Mysqli("host","db user","db password","db name");

//conect with database

For how to check whether the connection was established successfully, see http://docs.php.net/manual/en/mysqli.construct.php .

Upvotes: 2

user3744076
user3744076

Reputation: 127

Thank you @Epodax for reminding that. Just changed my config to $mysqli.

<?php 
$mysqli = new mysqli('localhost','root','sa','company');
if($mysqli->connect_errno){
echo "Connection Failed".$mysqli->connect_error;
}
?>

Upvotes: 0

Related Questions