Simon A. Callaghan Jensen Simon A. Callaghan Jensen - 28 days ago 5
PHP Question

PHP JSON_Encode() returns NULL with UTF-8 (Array within an Array)

I'm trying to retrieve an array from my database, but keep getting returned NULL values or nothing at all.

I have tried converting the entire database with UTF-8 unicode, numerous different methods, and hosting sites.

The response i am getting with print_r and json_encode is as follows:

Array
(
[success] =>
[0] => Array
(
[lat] =>
[lng] =>
[value] =>
)

[1] => Array
(
[lat] =>
[lng] =>
[value] =>
)

[2] => Array
(
[lat] =>
[lng] =>
[value] =>
)

[3] => Array
(
[lat] =>
[lng] =>
[value] =>
)

[4] => Array
(
[lat] =>
[lng] =>
[value] =>
)

)

0No error{"success":false,"0":{"lat":null,"lng":null,"value":null},"1"{"lat":null,"lng":null,"value":null},"2":{"lat":null,"lng":null,"value":null},"3":{"lat":null,"lng":null,"value":null},"4":{"lat":null,"lng":null,"value":null}}


My php code:

<?php
header('Content-Type: application/json');
$con = mysqli_connect("aaa", "bbb", "ccc", "ddd");

//Sets charset for JSON parsing
//no need anymore, database is utf8_unicode_ci already
//$charset = mysql_query("SET CHARACTER SET utf8");
//utf8_encode($arrItem);

$statement = mysqli_prepare($con, "SELECT * FROM markers ORDER BY marker_id");
mysqli_stmt_execute($statement) or die(mysqli_error($con));

$arrRows = array();
$arryItem = array();
$arrRows["success"] = false;
$arryItem["success"] = false;

mysqli_stmt_bind_result($marker_id,$lat,$lng,$value);

while(mysqli_stmt_fetch($statement)) {
$arrRows[] = compact('marker_id','lat','lng','value');
}

print_r($arrRows);
echo json_last_error();
echo json_last_error_msg();
echo json_encode($arrRows);
?>


The values supposed to be returned are these:

database values

All help is much appreciated, really need to solve this!

If there is something i didn't explain well enough or you need additional info, please let me know. I could post the other methods i tried for my while loop, but was told that using compact() was the most optimal way to do it.

Answer

You need to pass $statement as the first argument to your mysqli_stmt_bind_result() call when using it in a procedural way.

Comments