user4944440
user4944440

Reputation:

PHP->JSON Encoding not working

I know this has been asked like a million times now.

I tried several solutions I found here but still it doesn't work for me.

What i want to do is SELECT Values out of a simple MySQL Table.

The Values are inserted every five minutes by a program I have written.

I catches all mp3 files in a selected folder and inserts its ID3 Tags into the Table tb_song.

These files should then be SELECTED with the PHP Script and an Android App should Play these files with their URL.

The Database and PHP Code works.

If I just echo all selected values it works fine. But converting and printing out the encoded array just throws an blank screen.

Could it be that JSON Objects are limited to size?

I've got about 500 entries in tb_song.

Here's my code.

<?php
require_once('config.php');
$connection=new mysqli($server,$user,$password,$database);

$songs=array();
$sql=("SELECT Title,Artist,Album FROM tb_song");
$result=$connection->query($sql);

while($row=$result->fetch_assoc())
{
$temp=array();

$temp['Title']=$row['Title'];
$temp['Artist']=$row['Artist'];
$temp['Album']=$row['Album'];

array_push($songs,$temp);

}
json_encode($songs);
echo(json_encode($songs));//just for testing purposes
  $connection->close();
?>

Upvotes: 0

Views: 634

Answers (2)

user4944440
user4944440

Reputation:

I finally figured it out. I guess this is not the standard which's happening to all people but anyway. Before I'll post my code I want to say a few things for people who are running into the same problem:

  1. Make sure you're only passing strings without 'ä','ü' or whatever letter that is not in the english alphabet.

  2. You need to give your JSON Object a Name, otherwise it could cause problems.

        <?php
        require_once 'config.php';
        $connection = new mysqli($server,$user,$password,$database);
    
    
        if ($connection->connect_error) {
            die('Connect Error (' . $connection->connect_errno . ') '
                    . $connection->connect_error);
        }
    
    
        $songs=array();//Create Array
    
        $sql = 'SELECT * FROM tb_song';
        $result = $connection->query($sql);
    
        while($row=$result->fetch_assoc()){
    
    
    
        array_push($songs,$row);//Insert $row in $songs
    
        }
    
        echo json_encode(array('Songs'=>$songs));//Giving JSON Object a proper Name and //encode
    
    
    
        $connection->close();
        ?>
    

Upvotes: 0

RiggsFolly
RiggsFolly

Reputation: 94662

You can distil your code down to this. Plus adding some error checking!

<?php
/* add next 2 lines while testing, 
   especially if you are using a live hosting site
   where error reportinf will be turned off
*/
error_reporting(E_ALL); 
ini_set('display_errors', 1);

require_once 'config.php';
$connection = new mysqli($server,$user,$password,$database);

// Check connection is good
if ($connection->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') '
            . $connection->connect_error);
}

$songs=array();

$sql = 'SELECT Title,Artist,Album FROM tb_song';
$result = $connection->query($sql);

if ( ! $result ) {
    echo $connection->error;
    exit;
}

while($row=$result->fetch_assoc()) {
    $songs[] = $row;
}

$jstring = json_encode($songs);
if ( json_last_error() > 0 ) {
    file_put_contents('json-output.txt', json_last_error_msg());
}
echo $jstring;

//add this line for testing
file_put_contents('json-output.txt', $jstring);
exit;
?>

Upvotes: 1

Related Questions