1

I was learning to use JSON on PHP. I have a DB with a table that holds records of NightClubs, and also a NightClub class.

So far so good, no errors encountered, but when I want to echo the json_encode() It doesn't show me any information.

I've tried to echo son_last_error(); and it returned "0".

I also tried using:

mysql_query('SET CHARACTER SET utf8');

But it did't work.

Here is the code:

<?php 
header('Contennt-Type: application/json');
require_once("./inc/Event.inc.php"); //the Boliche object
require_once("./inc/connect.inc.php");//connections to DB

if(mysqli_connect_errno()){
    printf("Connect faild: %s\n", mysqli_connect_error());
    exit();
}
mysql_query('SET CHARACTER SET utf8');

$query = "SELECT * FROM boliches WHERE id IN (1,2)";

$boliche_array = array();

if($result = $dbc->query($query))
{
    while($obj = $result->fetch_object())
    {
        printf("%s %s %s %s %s %s %s %s </br>", $obj->id, $obj->bname,$obj->place,$obj->OpenTime,$obj->phone,$obj->cost,$obj->age,$obj->website);

        $temp_boliche = new Boliche($obj->id, $obj->bname,$obj->place,$obj->OpenTime,$obj->phone,$obj->cost,$obj->age,$obj->website);

        $boliche_array[] = $temp_boliche;

}

echo "<br /><br />";


echo '{"students":[';


$dale_data = json_encode($boliche_array[0]);
echo $dale_data;


echo ',<br />';

$dale_data = json_encode($boliche_array[1]);
echo $dale_data . "<br />";


echo ']}';

// Close the database connection
$result->close();
$dbc->close();

}
?>

And this is what I get on my screen

{"students":[{},
{}
]}
  • Why is your data *not* something json_encode supports? Also, please don't bastardize JSON generation - serialize a single aggregate tree. – user2864740 Aug 30 '15 at 20:18
  • 1
    You will probably be interested in something http://benwerd.com/2012/08/29/serializing-php-objects-into-json-with-jsonserializable/ or http://stackoverflow.com/questions/7005860/php-json-encode-class-private-members or if wishing to serialize custom objects. – user2864740 Aug 30 '15 at 20:20
  • @mario sory for the utf8size(), I was trying different solutions, I will edit it now. About the print_r, it printed the array successfully, no errors encountered. – user3187898 Aug 30 '15 at 20:34
  • Why are you trying to JSON encode a PHP class object? – Charlotte Dunois Aug 30 '15 at 20:43
  • @CharlotteDunois "Because it's a common task supported by many other languages that avoids a manual array-object construction step" .. ? Just because json_encode doesn't work like this (without some help) doesn't mean it isn't a valid request; although the 'request' is buried in the debug-this question with a lot of spurious information. – user2864740 Aug 31 '15 at 04:26
  • Also see http://stackoverflow.com/a/4697671/2864740 – user2864740 Aug 31 '15 at 04:32

0 Answers0