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();
?>
hatef
  • 5,491
  • 30
  • 43
  • 46
  • 1
    A white page is usually a server error, turn on error reporting and see what's going on. http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php – JimL Jan 26 '16 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. – Marc B Jan 26 '16 at 21:19
  • 1
    why is this question tagged as android? – Gueorgui Obregon Jan 26 '16 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. –  Jan 26 '16 at 21:26
  • It's tagged as Android because the PHP Scipt is used to deliver the JSON Array to my Android App. Sorry if this Tag is wrong! –  Jan 26 '16 at 21:27
  • 2
    You just need `$songs[] = $row` – RiggsFolly Jan 26 '16 at 21:27
  • Do I need to add an index? Like songs[x]=$row; x++; ? –  Jan 26 '16 at 21:29
  • @Needforbleed No, you just need to do `$songs[] = $row;`. You should add error checking to see if the connection created an error and check that your query executed correctly. An example can be found in the mysqli_result documentation. – Logan Bailey Jan 26 '16 at 21:31
  • Remove everything inside the brackets and copy&paste what Riggs has written above your comment. – hatef Jan 26 '16 at 21:31

2 Answers2

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;
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 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 –  Jan 26 '16 at 21:37
  • 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? – RiggsFolly Jan 26 '16 at 21:38
  • 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. –  Jan 26 '16 at 21:40
  • I guess you just named it false. –  Jan 26 '16 at 21:41
  • Ok spotted the copy/paste error, it gets me every time, try now – RiggsFolly Jan 26 '16 at 21:43
  • No problem. I already tried it with your way but neither I'm getting any erros nor an output. –  Jan 26 '16 at 21:45
  • Where are you looking for the output. Added a line to output the json string to a file – RiggsFolly Jan 26 '16 at 21:50
  • Hmmmm, this is fighting back!! – RiggsFolly Jan 26 '16 at 22:00
  • There must be an error within the encoding process. print_r($songs); works fine –  Jan 26 '16 at 22:00
  • Ok so I added some error checking on the json_encode statement, does that show anything in the output file, note last few lines all changed – RiggsFolly Jan 26 '16 at 22:05
  • I like your solution, gave you an up vote. I use the list with fetch_row The reason I use fetch_row() is because numeric arrays are faster than associative, and it keeps the SQL result smaller. I like using the list because the variable names make sense. It may not be the applicable for this app. In your solution I would still use fetch_row() and in favor of integer rather than verbose associative index. – Misunderstood Jan 26 '16 at 22:07
  • I can't believe it! The .txt file is completely empty –  Jan 26 '16 at 22:09
  • @Misunderstood But then you create an array and give them names, you think that is less overhead than just using the one array created by a `C` extension? I somehow doubt it – RiggsFolly Jan 26 '16 at 22:10
  • NeedForBleed: This makes no sence at all. I am runing out of hair – RiggsFolly Jan 26 '16 at 22:11
  • Yea. I'll do a new installation of xammp! –  Jan 26 '16 at 22:12
  • You are correct, I use the list out of habit, that is why I said it's not applicable here. Still I would use fetch_row() and not use named indexes in the JSON output. – Misunderstood Jan 26 '16 at 22:18
  • Could this all happen due to a misconfigured xammp environment ? I did not change anything in the configs. –  Jan 26 '16 at 22:20
  • Add a line of code to retrieve the row count of $result instead of ! $result. ! $result will only get a query failure and not catch no rows returned. – Misunderstood Jan 26 '16 at 22:25
  • I do not believe XAMPP is the problem. You would have a connection error if that was the case. – Misunderstood Jan 26 '16 at 22:37
  • That is what I have thought ,too. But where's the error then. –  Jan 26 '16 at 22:40
  • Add these two lines after the query: if(result> 0){echo 'ERROR: ' . result->error;} and if($result->num_rows == 0){echo 'No Rows';} – Misunderstood Jan 26 '16 at 22:50
  • I already did that. the array ($songs) contains all the elements of the query. But when it comes to converting the array to a JSON it just throws out an empty JSON Object –  Jan 26 '16 at 22:51
  • You did a check to make sure $songs has values? var_export($songs) – Misunderstood Jan 26 '16 at 23:04
  • What about if($jstring === false) and trying json_encode($songs,JSON_PARTIAL_OUTPUT_ON_ERROR) – Misunderstood Jan 26 '16 at 23:08
  • Nope.Yeam $songs does have a value. But maybe I just found the problem. The Entries are containing letters like 'ä' and 'ü'. Maybe that causes a problem. –  Jan 26 '16 at 23:28
  • It actually makes a difference. When passing string containing letters like 'ä'< the letter gets replaced by the typical HTML questionmark. Maybe that causes the encoder to crash. I'll check that now! –  Jan 26 '16 at 23:41
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();
        ?>