如何在PDO和JSON中处理多行数据

时间:2016-03-31 18:12:20

标签: php mysql json pdo

这是我的最后一年项目,我的教授要我写和Android应用程序。但是,我只是java的初学者。

我刚写了一个PHP,然后返回

  

致命错误:未捕获错误:在第18行的C:\ wamp64 \ www \ android \ input_course_list_load.php中调用boolean上的成员函数fetch_assoc()   错误:在第18行的C:\ wamp64 \ www \ android \ input_course_list_load.php中调用boolean上的成员函数fetch_assoc()

原始讯息:

<br />                                                           
<font size='1'><table class='xdebug-error xe-uncaught-exception' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in C:\wamp64\www\android\input_course_list_load.php on line <i>18</i></th></tr>
    <tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Error: Call to a member function fetch_assoc() on boolean in C:\wamp64\www\android\input_course_list_load.php on line <i>18</i></th></tr>
    <tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>     
    <tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
    <tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0005</td><td bgcolor='#eeeeec' align='right'>372576</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\android\input_course_list_load.php' bgcolor='#eeeeec'>...\input_course_list_load.php<b>:</b>0</td></tr>
</table></font>

使用原始代码input_course_list.php

<?php
// load course list for user to edit
// or let user to add course
require_once 'include/DB_Connect.php';
$db = new DB_Connect();
$conn = $db->connect();

$count = 0;

$response = array("error" => false);

if(isset($_POST['username']))   {
    $username = $_POST['username'];
    $stmt = $conn->prepare("select * from course where username=?");
    $stmt->bind_param('s', $username);
    $stmt->execute();
    $response["error"] = false;
    while($result = $stmt->get_result()->fetch_assoc()) {

        $response[$count]["course_id"] = $result["course_id"];
        $response[$count]["course_code"] = $result["course_code"];
        $response[$count]["course_name"] = $result["course_name"];
        $response[$count]["course_type"] = $result["course_type"];

        $count++;
}
$stmt->close();

if($count != 0) {
    echo json_encode($response);
} else  {
    $response["error"] = true;
    $response["error_msg"] = "User have enrolled 0 course";
    echo json_encode($response);
}

}  else {
    $response["error"] = true;
    $response["error_msg"] = "Required data not given (username)";
    echo json_encode($response);
}

我的查询

select * from course where username='test'

My database table course

Expected result

如何正确使用PDO检索和处理多行数据?

0 个答案:

没有答案