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

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

最佳答案

如果你想返回一个包含所有查询结果的数组,那么使用 PDOStatement 类的 fetchAll() 方法

public function getEvent(){ 

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

关于php - 如何使用php从mysql数据库中检索所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36817300/

相关文章:

php - 无法在 Windows Server 2016 中的 WordPress 中创建目录 wp-content

PHP Mysql 跟踪访问者的可扩展性

php - 使用 PHP 访问文件夹并选择文件名

android - 使用 Android Studio 生成 Pro Guard 配置文件

mysql - 使用外键从多个表插入数据

php - cakephp 编辑字段值

php - 如何从显示的YouTube视频的链接中获取jQuery变量到PHP以发送到数据库?

PHP:让您自己的函数使用 while 循环

Android WebView CSS float

android - ListView clickEvent 选择了错误的数据库行 ID - 帮助!