转换为 PDO 后数据不显示在 ListView

我已经创建了一个包含 ListView 的应用程序。我使用 PHP 连接 android 和数据库。现在,我使用 MySQLi 并且它可以工作。但是当我转换为 PDO 时,数据不显示。我使用“Log.d”来跟踪数据的内容。结果如下:

  1. MySQLi(无错误)- {"data":[{"report_id":19,"task_name":"ngantuk","badgeid":"12345","report_date":"04 Dec 2019",..... ..

  2. PDO(不记录显示数据)

现在,下面是 MySQLi 和 PDO 的当前代码

MySQLi

<?php 


    require_once 'config.php';


    if (mysqli_connect_errno()) {

        echo "Failed to connect to MySQL: " . mysqli_connect_error();

        die();

    }


    $badgeid = $_GET["badgeid"] ?? "";


    $stmt = $conn->prepare("SELECT report_id, task_name, badgeid, report_date, photo_before, photo_after, report_status FROM report WHERE badgeid = '$badgeid' AND report_status = 'Pending';");

    $stmt->execute();

    $stmt->bind_result($report_id, $task_name, $badgeid, $report_date, $photo_before, $photo_after, $report_status);


    $task = array(); 


    while($stmt->fetch()){

        $temp = array();

        $temp['report_id'] = $report_id; 

        $temp['task_name'] = $task_name; 

        $temp['badgeid'] = $badgeid;


        $booked = strtotime($report_date);

        $report_date = date("d M Y", $booked);


        $temp['report_date'] = $report_date;

        $temp['photo_before'] = $photo_before;

        $temp['photo_after'] = $photo_after;

        $temp['report_status'] = $report_status; 

        array_push($task, $temp);

    }


    $response = array();

    $response["data"] = $task;

    echo json_encode($response);


?>

有谁知道我的 PDO 代码有什么问题?



慕桂英4014372
浏览 93回答 1
1回答

慕勒3428872

mysqli 代码和 PDO 代码不一样。我将代码从 mysqli 版本复制到 PDO 版本。PDOStatement::fetch()一次得到一排。当您从 fetch 中获取结果并将其放入 while 循环的条件中时,您正在创建一个无限循环,最终将耗尽内存。require_once 'configPDO.php';$badgeid = $_GET["badgeid"] ?? "";$stmt = $conn->prepare("SELECT report_id, task_name, badgeid, report_date, photo_before, photo_after, report_status FROM report WHERE badgeid = :badgeid AND report_status = 'Pending'");&nbsp; &nbsp;$stmt->bindParam(':badgeid',$badgeid,PDO::PARAM_STR);$stmt->execute();$task = [];while ($result = $stmt->fetch(PDO::FETCH_ASSOC)) {&nbsp; &nbsp; $temp = [];&nbsp; &nbsp; $temp['report_id'] = $result['report_id'];&nbsp;&nbsp; &nbsp; $temp['task_name'] = $result['task_name'];&nbsp;&nbsp; &nbsp; $temp['badgeid'] = $result['badgeid'];&nbsp; &nbsp; $booked = strtotime($result['report_date']);&nbsp; &nbsp; $report_date = date("d M Y", $booked);&nbsp; &nbsp; $temp['report_date'] = $result['report_date'];&nbsp; &nbsp; $temp['photo_before'] = $result['photo_before'];&nbsp; &nbsp; $temp['photo_after'] = $result['photo_after'];&nbsp; &nbsp; $temp['report_status'] = $result['report_status'];&nbsp;&nbsp; &nbsp; $task[] = $temp;}&nbsp; &nbsp;$response = [];$response["data"] = $task;echo json_encode($response);
打开App,查看更多内容
随时随地看视频慕课网APP