0

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();
?>
9
  • 1
    A white page is usually a server error, turn on error reporting and see what's going on. stackoverflow.com/questions/845021/… Commented Jan 26, 2016 at 21:18
  • 2
    note that your $temp array is rather pointless. the array already contains only those 3 fields, so why not just while(...) { array_push($songs, $row); }? plus the first json_encode() call is useless as well. you don't catch the return value, so you're doing the double the work and throwing away half of it. Commented Jan 26, 2016 at 21:19
  • 1
    why is this question tagged as android? Commented Jan 26, 2016 at 21:25
  • I removed $temp, enabled error reporting and changed array_push($songs,$temp) to array_push($songs,$row). But still just an empty page's coming out. Commented Jan 26, 2016 at 21:26
  • 2
    You just need $songs[] = $row Commented Jan 26, 2016 at 21:27

2 Answers 2

1

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;
?>
Sign up to request clarification or add additional context in comments.

26 Comments

1. Your Script contains a few errors. And 2. After correcting the errors it's still not working. What am I doing wrong ? But I'm thankful anyway
My script does not contain value in these variables $server,$user,$password,$database because neither did yours! My clairvoyant skills are waning. Or are there other errors?
That's not the error. I'm doing it OOP. So you cannot use mysqli as a method here.You need to replace every mysqli with the mysqli connection object called $connection in my case. That's what you did wrong.
I guess you just named it false.
Ok spotted the copy/paste error, it gets me every time, try now
|
0

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();
        ?>
    

Comments

Your Answer

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