2

I am trying to return the result of a mysql query as json, but the json_encode function always returns a blank although the array contains elements.

Here is the relevant code

Retrieve the query and convert to php array:

$sql = "blablabla";
$clients = mysql_query($sql, $con);

$result = array();
while($row = mysql_fetch_assoc($clients)) {
    //echo "rij: " . $row['name'];
    $result[] = $row;
}

Then I return the result:

echo json_encode($result);

This returns nothing, while I do get results when echoing the following statements:

echo count($return);

returns 1

echo $return[0];

returns "Array"

echo $return[0]['name'];

returns "YOUREKA! demo"

Any idea what I am doing wrong?

Edit: I have one record that is and should be returned which contains following data:

id: 2, name: YOUREKA! demo, address: Italiëlei 26, postalcode: 2000, city: Antwerpen, country: België, countrycode: BE, telephone: +32-476061346, email: [email protected], distance: 30.762980999827132,

8
  • json_encode returns false on error. What does json_last_error_msg say? Commented Feb 20, 2016 at 13:42
  • Can you edit post adding your JSON (undecoded) string? Commented Feb 20, 2016 at 13:43
  • U r saving data into an array $result[] Commented Feb 20, 2016 at 13:43
  • $result[] = $row['name']; Commented Feb 20, 2016 at 13:45
  • I want to include all the columns of the recordset in the json, not just the "name" column Commented Feb 20, 2016 at 13:46

2 Answers 2

0

The documentation on json_encode() states "All string data must be UTF-8 encoded. ". Your attempts to use mysql_set_charset is the right kind of thinking.

Use this kind of debuggery:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "test");

printf("Initial character set: %s\n", $mysqli->character_set_name());

/* change character set to utf8 */
if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
    exit();
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}

$mysqli->close();
?>
Sign up to request clarification or add additional context in comments.

Comments

0

I write a demo of php/mysql connection, with json data: Working fine

<?php
// we connect to localhost
$link = mysql_connect('localhost', 'username', 'password');
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
if (!mysql_select_db("db_name")) {
    echo "Unable to select mydbname: " . mysql_error();
    exit;
}
$query = 'SELECT * FROM users where id = 1';
//Query database
$result = mysql_query ($query);
$arr = array();
//Iterate result
while ($record = mysql_fetch_assoc ($result)){
    $arr[] = $record;
    // print_r ($record);
  }
echo "<pre>".print_r($arr,1)."</pre>";
echo json_encode($arr);
mysql_close($link);
?>

Note : This extension was deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.