I am working on a school assignment where I have to take a Twitter dump as an SQL and save it as a JSON (to be used for D3). I am having some issues with the JSON encoding, however. I was able to get date, user, retweets, favorites to all function properly, but for some reason, the actual text of the tweet is only encoding as "text". I've checked the table and everything on there looks correct, but for some reason, my encoder is not encoding the text correctly. Here is the code I have to encode the Twitter SQL table into JSON:
<?php
$dsn = 'mysql:host=localhost;dbname=twitter';
define('MYSQL_USER','root');
define('MYSQL_PASS','dismypassword');
$tweets = "SELECT `id`,`date`,`screen_name`,`favorite_count`,`favorited`,'text' as 'textThing' FROM tweets";
$sql = $tweets;
header("Content-Type: application/json");
try {
$db = new PDO($dsn, MYSQL_USER, MYSQL_PASS);
$statement = $db->prepare($sql);
$statement->execute();
$data = array();
while ( ($row = $statement->fetch(PDO::FETCH_ASSOC)) ) {
$data[] = $row;
}
echo json_encode($data);
} catch (PDOException $e) {
die('Query failed: ' . $e->getMessage());
}
?>
The output from this is generating a JSON file that looks like this(where the text is coming up as simply "text":
[
{
id: "704419321326510080",
date: "2016-02-29 22:33:48",
screen_name: "cscstars",
favorite_count: "0",
favorited: "0",
textThing: "text"
},
{
id: "704419329287331840",
date: "2016-02-29 22:33:50",
screen_name: "Wary12",
favorite_count: "0",
favorited: "0",
textThing: "text"
},
{
id: "704419365588865024",
date: "2016-02-29 22:33:58",
screen_name: "lgib15",
favorite_count: "0",
favorited: "0",
textThing: "text"
},
{
id: "704419366243151874",
date: "2016-02-29 22:33:59",
screen_name: "jacqui4peace",
favorite_count: "0",
favorited: "0",
textThing: "text"
},