dsztc99732 2018-01-28 17:23
浏览 90
已采纳

从数据库中获取,如JSON

I am trying to fetch data from my database, but when I visit MYWEBPAGE.com/service.php?id=2 (yes, there is an article with that id), then it just writes: [true], instead for showing me the objects as json.

Here is my code:

<?php
 

include('includingThis.php');

$idFromUrl = addslashes($_GET[id]);

$resultArray = array();
$tempArray = array();
 
if ($stmtTitle = $con->prepare("SELECT * FROM artikler WHERE id=?")) {
    
    
                
                /* bind parameters for markers */
                $stmtTitle->bind_param("i", $idFromUrl);
                
                $stmtTitle->execute();
                    
                // Loop through each row in the result set
                while($row = $stmtTitle->fetch()) {
                        
        // Add each row into our results array
        $tempArray = $row;
        array_push($resultArray, $tempArray);
    }
 
    // Finally, encode the array to JSON and output the results
    echo json_encode($resultArray);
}
 
// Close connections
mysqli_close($con);
?>

</div>
  • 写回答

2条回答 默认 最新

  • duandao1931 2018-01-28 17:37
    关注

    The trouble you had is with using fetch. It does not return a row, it returns a boolean. If you wish to get a row of * fields from a prepared statement (using mysqli), you can do it one of two ways.

    1) If you have mysqlnd available:

    You utilize get_result first, then fetch_assoc:

    <?php
    include('includingThis.php');
    $resultArray = array();
    if ($stmtTitle = $con->prepare("SELECT * FROM artikler WHERE id=?")) {
        $stmtTitle->bind_param("i", $_GET['id']);// just put your GET var here
        $stmtTitle->execute();
        $stmtTitle_result = $stmtTitle->get_result();// get the result object
        while($row = $stmtTitle_result->fetch_assoc()) {
            $resultArray[] = $row;// add each row into resultArray directly
        }
        echo json_encode($resultArray);
    }
    ?>
    

    2) If you do NOT have mysqlnd available:

    You utilize bind_result but must know every field you need: (change fieldnames to what YOUR field names are, not my examples of field and field2)

    <?php
    include('includingThis.php');
    $resultArray = array();
    if ($stmtTitle = $con->prepare("SELECT field1,field2 FROM artikler WHERE id=?")) {
        $stmtTitle->bind_param("i", $_GET['id']);// just put your GET var here
        $stmtTitle->execute();
        $stmtTitle->bind_result($field1,$field2);// bind to each field
        while($stmtTitle->fetch()) {
            $resultArray[] = array('field1'=>$field1,'field2'=>$field2);// add each field
        }
        echo json_encode($resultArray);
    }
    ?>
    

    Incidentally, this is a bit easier (cleaner) using the PDO library.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化