我的sqli
请求没有结果。。像空数据一样。。我很确定我的数据库里有很多数据
这是我的代码,如果我的代码有错误,请纠正我
<?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
发送此数据以在我的应用程序中使用。。。
我认为这里不需要准备好的语句,因为您没有参数化任何数据。尝试一个简单的查询,而不是
$stmt = $this->conn->query("SELECT cPID, image FROM sliderImage");
$response = array();
while($result = $stmt->fetch_assoc()) {
$response[] = $result;
}
echo json_encode($response);
return true;