Shino Rascal Shino Rascal - 4 months ago 9
PHP Question

No result on PHP value Mysqli stmt

there's no result on my

sqli
request .. like empty data.. i'm pretty sure in my database there's much data

here's my code, correct me if i got mistake on my code

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

?>


my DB request to connect

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

?>


and
config.php
file

<?php

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


i want to put the result into array.. and send this data using
json_encode
to use it in my app...

Answer

I don't think you need a prepared statement here, since you're not parameterizing any data. Try a simple query instead

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