-1

mySQL を JSON にエンコードしようとしています。私はこの人気のあるリンク JSONでMySQLの結果をエンコードしようとしましたが、答えはうまくいきませんでした. 私はあなたの助けに感謝します。

これが私のコードです:

<html>
<body>

<?php
    header('Content-Type: text/html; charset=utf-8');
    $con=mysqli_connect("localhost","root","root","Theory");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT `questions`.`questionID` AS question,
                                        `questions`.`questionText` AS questionText,
                                        `questions`.`categoryID` AS categoryID,
                                        `answers`.`answerID` AS answerID,
                                        `answers`.`answerText` AS answerText,
                                        `answers`.`isTrue` AS isTrue
                                 FROM `questions`,`answers`
                                 WHERE `questions`.`questionID`=`answers`.`questionID`");

    if (!$result)
    {
        die('Error: ' . mysqli_error($con));
    }

   $rows = array();
   while($r = mysql_fetch_assoc($result)) {
   $rows[] = $r;
   }
   print json_encode($rows);

    mysqli_close($con);
    ?>

</body>
</head>

私は取得しています:"[]" 私が愚かなことをしている場合は申し訳ありません。私はphpに慣れていません。

4

3 に答える 3