我有一个问题。下面的代码在我的本地工作得很好,但在实时服务器上,它不能正常工作..我应该得到两行,但在实时服务器上,我只得到1个结果。
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$result = mysql_fetch_object($query);
print json_encode($result);
我真不敢相信你用这个得到了两行,要得到所有的行你必须这样做:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
print json_encode($result);
}
mysql_fetch_object/array/row总是只返回一行并将指针移动到下一行,如果没有下一行则返回false
您的代码只获得一行。mysql_fetch_object()
函数只返回一行。您需要尝试这样做:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$json = array();
while ($result = mysql_fetch_object($query))
$json[] = $result;
print json_encode($json);
我认为这是因为mysql_fetch_object只返回单行结果。你需要这样做:
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($row = mysql_fetch_array($query))
{
''access each result here
}
我不明白你怎么能在本地有两行
$array = array();
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
$array[] = $result;
}
print json_encode($array);