未定义的变量:mysqli&在null上调用成员函数query()

时间:2015-12-02 11:58:26

标签: php mysqli

我是mysqli的新手,我正试图在桌子上显示数据。

以下是代码:

<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>

这些是错误:

  

未定义的变量:第205行的C:\ xampp \ htdocs \ company \ admin \ user_table.php中的mysqli

     

致命错误:在第205行的C:\ xampp \ htdocs \ company \ admin \ user_table.php中调用null上的成员函数query()

2 个答案:

答案 0 :(得分:2)

未定义的变量:mysqli意味着你没有创建对象&#39; mysqli&#39;或者无法获得对象mysqli;即,你必须establish a connection通过creating an instance mysqli 之前http://docs.php.net/manual/en/mysqli.construct.php到MySQL服务器,你可以使用这个实例向服务器发送查询。

  

创建对象

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

//与数据库结合

  

如何检查连接是否成功建立,   见https://jsfiddle.net/demas/9jLayt3v/

答案 1 :(得分:0)

感谢@Epodax提醒。刚刚将我的配置更改为$ mysqli。

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