如何使用php从mysql数据库中检索所有数据?

时间:2016-04-23 22:44:27

标签: php android mysql listview wampserver

我有另一个php文件,它具有连接数据库的功能(这样做成功),还有一个包含sql语句的函数getEvent。 下面的代码成功从数据库中检索一个数据行,但我需要它从表中返回所有数据。

<?php
// include db connect class
require_once 'include/DB_Functions.php';

// connecting to db
$db = new DB_Functions();

 // array for JSON response
$response = array();

// get all events from events table
$event = $db->getEvent();

// check for empty result
if($event !=FALSE){


    $response["error"]=FALSE;
    $response["uuid"] = $event["eventID"];
        $response["eventdetails"]["title"] = $event["title"]; 

echo json_encode($response);

}else{
$response["error"]=TRUE;
$response["error_msg"] = "No events to display";
        echo json_encode($response);
}

?>

getEvent方法

public function getEvent(){ 
    $stmt= $this->conn->prepare("SELECT * from eventdetails"); 
    $stmt->execute(); 
    $event= $stmt->store_result(); 
    if ($stmt->execute()) { 
        $event = $stmt->get_result()->fetch_assoc(); 
        $stmt->close(); 
        return $event; 
    } else { 
        // events dont existed 
        $stmt->close(); 
        return null; 
    }

1 个答案:

答案 0 :(得分:0)

如果您想返回包含查询中所有结果的数组,请使用PDOStatement类的fetchAll()方法

public function getEvent(){ 

    $stmt = $this->conn->prepare("SELECT * from eventdetails"); 
    $stmt->execute(); 
    $events = $stmt->fetchAll(); 
    return $events;
}