I realize I'm using mysql and not mysqli. I need to solve this issue before I can update the existing code. I currently querying data as well as the table row names from the database via php. I cant figure out why I am not receiving a response when I run the json_encode. Can someone please help me out?
My desired output in JSON format is as follows..
{ "projects" : [
{
"title":"title 1",
"image":"Image1",
"subtitle":"subtitle 1",
"about":"Blah Blah Blah"
},
{
"title":"title 2",
"image":"Image2",
"subtitle":"subtitle 2",
"about":"Blah Blah Blah"
}
]}
Right now my code looks like this..
// 1) Connect to mysql database
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
// 2) Query database for data
$result = mysql_query("SELECT * FROM $tableName");
if (mysql_num_rows($result) > 0)
{
$numFields = mysql_num_fields($result);
$fields = array();
$i = 0;
while ($i < $numFields) {
$fieldInfo = mysql_fetch_field($result, $i);
$fields[] = $fieldInfo->name;
$i++;
}
$userInfo = array();
$userInfo["projects"] = array();
while($row = mysql_fetch_row($result)) {
$project = array();
$i = 0;
while ($i < mysql_num_fields($result)) {
$project[$fields[$i]] = $row[$i];
$i++;
}
$userInfo["projects"][] = $project;
}
//echo '<pre>';
// print_r($userInfo);
//echo '</pre>';
echo json_encode($userInfo["projects"]);
}
else
{
echo ( "No rows found!" );
}
When I execute the echo '<pre>'
above (currently commented out) I get the following result. However when I remove this and try return the data as JSON I get no result. Could someone please help me out.
Array
(
[projects] => Array
(
[0] => Array
(
[title] => title
[subtitle] => subtitle 1
[about] => Blah Blah Blah
[image] => Image1
)
[1] => Array
(
[title] => title 2
[subtitle] => subtitle 2
[about] => Blah Blah Blah
[image] => Image2
)
)
)