1

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

4
  • pass db connection in query : $mysqli->query($conn, "SELECT username,password,FName,LName,userAddress FROM tbl_users"); Commented Dec 2, 2015 at 12:01
  • @Krishna, no, $mysqli would be the connection resource, it's either the oop-style where the object is the connection resource or the procedual style where you pass the resource as a parameter. Commented Dec 2, 2015 at 12:03
  • 3
    If you haven't created a connection to your database, you need to do so. If you have, are you sure you named it $mysqli ? Commented Dec 2, 2015 at 12:03
  • Off topic, but this jumped out at me from your code: You should never have any reason to echo a user's password. If you're doing things properly, the passwords will hashed and unreadable anyway so no point outputting them. Commented Dec 2, 2015 at 12:24

2 Answers 2

2

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 .

Sign up to request clarification or add additional context in comments.

2 Comments

For how to check whether the connection was established successfully, see docs.php.net/mysqli.quickstart.connections . And an explaination why and how this is relevant to the question would be nice, too ;-)
@VolkerK you can vote up if my code is solve issue of Undefined variable: mysqli
0

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;
}
?>

1 Comment

You might want to add a bit more error handling. The remaining script can't "live" without the working connection, so, only printing the error message won't suffice (and might be dangerous). You should print a "sorry, unavailable" page instead of the users table in case something went wrong with the mysql connection. And $mysqli->query may also fail; you should add error handling there, too. if ( !$query ) { ....error handler ... } else { while( $row = ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.