我意识到我使用的是mysql而不是mysqli。我需要在更新现有代码之前解决此问题。我目前通过php查询数据以及数据库中的表行名称。我无法弄清楚为什么我在运行json_encode时没有收到响应。有人可以帮帮我吗?
我想要的JSON格式输出如下..
{ "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"
}
]}
现在我的代码看起来像这样..
// 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!" );
}
当我执行上面的echo '<pre>'
(当前已注释掉)时,我得到以下结果。但是当我删除它并尝试将数据作为JSON返回时,我得不到任何结果。有人可以帮帮我。
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
)
)
)