2016-07-31 48 views
2

有我sqli请求没有结果..像空的数据。我敢肯定,我的数据库中有多少数据PHP的价值没有结果的mysqli stmt是

这里是我的代码,纠正我,如果我上了错误我的代码

<?php 
    // include db handler 
class DB_Functions { 

    private $conn; 

    // constructor 
    function __construct() { 
     require_once 'include/DB_Connect.php'; 
     // connecting to database 
     $db = new Db_Connect(); 
     $this->conn = $db->connect(); 
    } 

     // destructor 
     function __destruct() { 

     } 

    function getSliderList(){ 

     $stmt = $this->conn->prepare("SELECT cPID, image FROM sliderImage"); 
     $stmt->execute(); 
     if ($stmt->num_rows > 0) { 
      $result = $stmt->get_result()->fetch_assoc(); 
      $response[] = $result; 
      $stmt->close(); 
      echo json_encode($response); 
      return true; 
     } else { 
      // user not found 
      return false; 
     } 
    } 
} 

     $x = new DB_Functions(); 
     $user = $x->getSliderList(); 
     $response = Array(); 
     if($user){ 
       $user; 
       return false; 
     } else { 
       $response['error'] = "Sorry an error occured. Our Problem, not you."; 
       return true; 
     } 

?> 

我的数据库请求连接

<?php 
class DB_Connect { 
    private $conn; 

    // Connecting to database 
    public function connect() { 
     require_once 'include/Config.php'; 

     // Connecting to mysql database 
     $this->conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE); 

     // return database handler 
     return $this->conn; 
    } 
} 

?> 

config.php文件

<?php 

/** 
* Database config variables 
*/ 
define("DB_HOST", "localhost"); 
define("DB_USER", "bxxx"); 
define("DB_PASSWORD", "xxxx"); 
define("DB_DATABASE", "xxxx"); 
?> 

我想结果放入数组..和使用json_encode在我的应用程序使用它发送此数据...

+0

你有错误报告吗? –

+0

不,没有什么结果,即使是一个错误... –

+0

为什么你准备不需要输入的语句 –

回答

2

我不认为你需要一个准备好的声明在这里,因为你不参数化任何数据。尝试一个简单的查询,而不是

$stmt = $this->conn->query("SELECT cPID, image FROM sliderImage"); 
$response = array(); 
while($result = $stmt->fetch_assoc()) { 
    $response[] = $result; 
} 
echo json_encode($response); 
return true; 
+0

我有218数据表中的数据库...但它只显示1数据..我想把所有的数据在数组中 –