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条)

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊