如何在Php中获取多条记录?

时间:2017-02-19 07:51:22

标签: php mysql mysqli

我正在尝试从数据库中获取多个记录,但是当我尝试多个记录时。我总是空着。

首先我尝试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);

回复:{“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”},{“问题”:null,“Option_2”:“10/1/2016 12:00:00 AM”} {“问题”:null ,“Option_2”:“10/1/2016 12:00:00 AM”}]}

但是,当我尝试所有记录并更改数据库查询以获取问题字段时,例如。

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

我的结果是空的,如“”。

我用于连接

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

2 个答案:

答案 0 :(得分:-1)

我认为你绑定的方式也是错误的:

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

答案 1 :(得分:-1)

试试这个

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

你可以得到答案..如果没有得到答案......让我知道