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 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么