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

为君一笑 提交于 2021-02-17 03:16:46

问题


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


回答1:


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 .




回答2:


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


来源:https://stackoverflow.com/questions/34042190/undefined-variable-mysqli-call-to-a-member-function-query-on-null

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!