Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Convert Data from MySQL to JSON using PHP

I am trying to convert data from my DB to JSON with a PHP script. Everything works great on my local environment (MAMP). As soon as I am trying to put things together on my server, my JSON File is empty :(

What I have so far

my php script

<?php

$connection = mysqli_connect("localhost","root","root","angulardb") or die("Error " . mysqli_error($connection));

$sql = "select * from postings"; $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;

}

$fp = fopen('menue.json', 'w'); fwrite($fp, json_encode($emparray)); fclose($fp);

echo json_encode($emparray);

?>

By uploading this script to my server (& update the data), I receive an empty JSON file with no response.

Any Idea what can be wrong??? Is there additional configuration necessary on the server side?

Thanks in advance

1 Answer

ok, got it! $pdo=new PDO("mysql:dbname=db239256x2330361;host=mysql.webhosting38.1blu.de;charset=utf8","s239256_2330361","20dj93lpav+dd");

the missing charset caused my problem