0

I am trying for getting multiple records from database but when I try for more then one records. I always getting empty.

First I try FROM DbOperation.php:

public function getDayListByDate($DateString){
$stmt = $this->con->prepare("SELECT DateString FROM gk_eng WHERE DateString= ?");
$stmt->bind_param("s",$DateString);
$stmt->execute();
$result = $stmt->get_result();
return $result;
}

php data get

<?php
require_once 'DbOperation.php';
$db = new DbOperation();
$DateString = $_POST['DateString'];
$devices = $db->getDayListByDate($DateString);
$response = array();
$response['error'] = false;
$response['devices'] = array();

while($device = $devices->fetch_assoc()){
$temp = array();
$temp['Question']=$device['Question'];
$temp['Option_2']=$device['DateString'];
array_push($response['devices'],$temp);
}
echo json_encode($response);

Response: {"error":false,"devices":[{"Question":null,"Option_2":"10/1/2016 12:00:00 AM"},{"Question":null,"Option_2":"10/1/2016 12:00:00 AM"},{"Question":null,"Option_2":"10/1/2016 12:00:00 AM"}{"Question":null,"Option_2":"10/1/2016 12:00:00 AM"}]}

But when I trying for all records and change database query for getting Question field in reponse like.

public function getDayListByDate($DateString){
$stmt = $this->con->prepare("SELECT * FROM gk_eng WHERE DateString= ?");
$stmt->bind_param("s",$DateString);
$stmt->execute();
$result = $stmt->get_result();
return $result;
}

I am getting result empty like "".

I am using for connection

   function connect()
   {
    //Including the constants.php file to get the database constants
    include_once dirname(__FILE__) . '/Config.php';

    //connecting to mysql database
    $this->con = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

    //Checking if any error occured while connecting
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    //finally returning the connection link 
    return $this->con;
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Kiran Maheshwari
  • 138
  • 1
  • 2
  • 9

2 Answers2

-1

I think the way you are binding is incorrect too:

public function getDayListByDate($DateString){
        $stmt = $this->con->prepare("SELECT * FROM gk_eng WHERE DateString= :DS");
        $stmt->bind_param(":DS",$DateString);
        $stmt->execute();
        $result = $stmt->fetchAll();
        return $result;
    }
mmta41
  • 274
  • 2
  • 13
  • Not working, I getting error. – Kiran Maheshwari Feb 19 '17 at 08:20
  • I test the code without getting error!, what is the error info and Do you use PDO for database connection? – mmta41 Feb 19 '17 at 08:31
  • I am Using function connect() { //Including the constants.php file to get the database constants include_once dirname(__FILE__) . '/Config.php'; //connecting to mysql database $this->con = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME); //Checking if any error occured while connecting if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } //finally returning the connection link return $this->con; } – Kiran Maheshwari Feb 19 '17 at 08:34
  • Sorry I miss-leaded you I thought you are using PDO connection but your connection type is mysqli and there is no fetchAll in mysqli connection – mmta41 Feb 19 '17 at 08:39
  • Now what can I do – Kiran Maheshwari Feb 19 '17 at 08:56
-1

Try This

function getDayListByDate($DateString) {
    $result = $this->con->query("SELECT * FROM gk_eng WHERE DateString='$DateString' ");
    return $result;
}

you can get answer.. if didnt get ans .. let me know

Kavin D
  • 474
  • 1
  • 4
  • 12