2017-10-14 193 views
-1

我从php中检索我的表中的数据。我的代码是在这里:Mysqli_query无法执行并抛出错误

<?php 
$con = require('config.php'); 
?> 

<html> 
<head><title>Php Practise Web</title></head> 
    <body> 
     <?php 
      $sqlQuery = 'SELECT * FROM customers'; 
      $customersData = mysqli_query($con,$sqlQuery); 
       if(!$customersData){ 
        die('Error in retrieving :'.mysqli_connect_error($con)); 
        } 
        else{ 
       while($dataRow = mysqli_fetch_array($customersData, MYSQLI_NUM)){?> 
     <table> 
      <tr> 
       <thead>Customer Name<thead> 
       <thead>Customer Email<thead> 
       <thead>Customer Username<thead> 
       <thead>Customer Age<thead> 
      </tr> 
      <tr> 
       <td><?php echo $dataRow['c_name']?></td> 
       <td><?php echo $dataRow['c_email']?></td> 
       <td><?php echo $dataRow['c_username']?></td> 
       <td><?php echo $dataRow['c_age']?></td> 
      </tr> 
     </table> 
    <?php 
       } 
      } 
    ?> 
    </body> 
    </html> 

但它抛出一个错误这样 这里是我的config.php代码

<?php 
$con = mysqli_connect("localhost","root","","phppractise"); 

if(mysqli_connect_errno()){ 
    echo "Failed to connect to Mysql : ".mysqli_connect_errno(); 
} 
else{ 
    echo "Database Connected"; 
    } 

>

enter image description here

从最初的

其实?我正在与我的数据库建立连接,然后从名为“customers”的表中检索数据。我在mysqli_query中传递连接变量和查询变量,但它仍然抛出一个错误。请告诉我我的代码错在哪里?

+1

发布'config.php'中的代码。它返回一个整数 - 读取错误。 –

+0

与您之前的问题有任何关系? https://stackoverflow.com/q/46739498/1415724 - 你应该编辑你的问题,以包含config.php文件中的内容。 –

+0

我甚至不知道require()*有*返回值。但看起来返回值是一个整数。 'mysqli_query()'期待一个'mysqli'连接对象。哪里是? – David

回答

1

您需要从这里取出$con =

$con = require('config.php'); 

这就是为什么你得到错误。

只要做到:

require('config.php'); 

或包含文件。

include('config.php'); 

该变量与您的连接相同。

要尝试解释这里发生了什么,它最有可能是循环遍历require()和连接中的变量赋值,这意味着它从返回的错误中返回1作为布尔值(整数)。

  • 这是我最好的假设,这对我来说是有道理的。

从错误的图像措施:

Warning: mysqli_query() expects parameter 1 to be mysqli integer given

为了使用语法类似($con = require('config.php');),你需要用一个类/方法,并从那里,到return的它的实例。