PHP code to encode DB data in JSON not working -
i'm stuck php code. want access table in database , retrieve data in json format. therefore, tried following code :
<?php $con = mysqli_connect("......","username","pwd","dbname"); if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $sql = "select * users"; if ($result = mysql_query($con, $sql)) { $resultarray = array(); $temparray = array(); while($row = $result->fetch_object()) { $temparray = $row; array_push($resultarray, $temparray); } echo json_encode($resultarray); } mysqli_close($con); ?>
however, it's getting me empty page. worked once special number of row in table, not efficient might guess.
does have idea why i'm getting weird results?
edit 1 :
i tried add code :
echo json_encode($resultarray); echo json_last_error();
and it's returning me 5. seems error data encoding in table. therefore added code :
$temparray = array_map('utf8_encode', $row) array_push($resultarray, $temparray);
and got following output : [null,null,null]0 (the 0 comes echo json_last_error();)
so here am, can me ?
i start changing if ($result = mysql_query($con, $sql))
if ($result = mysqli_query($con, $sql))
because different database extensions
another thing change while($row = $result->fetch_object())
while ($row = mysqli_fetch_object($result)) {
(procedural style vs. object oriented style)
if still see blank screen, try adding error_reporting(e_all);
@ top of script, , you'll able know bug is
Comments
Post a Comment