I am triyng to encode mySQL to JSON. I have tried this popular link JSON encode MySQL results but the answer didn't work for me. I'll appritiate your help.
Here is my code:
<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>
I am getting :"[]" Sorry if I am doing something stupid, I am new to php.