开发者

Why I am not getting all the result in my query code?

I am ha开发者_Go百科ving a problem. The following code works fine in my local, but on the live server, it's not working properly..I was supposed to get two rows, but on the live server I am getting only 1 result.

$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$result = mysql_fetch_object($query);
print json_encode($result);

What could possibly be the error ?...


I can't believe you get two rows with this, to get all rows you have to do like this:

$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
    print json_encode($result);
}

mysql_fetch_object/array/row always returns only one row and moves the pointer to the next row, if there is no next row it returns false.


Your code is only getting one row. The mysql_fetch_object() function only returns one row. You need to try something like this:

$query = mysql_query('SELECT * FROM `wspm_t_colors`');
$json = array();
while ($result = mysql_fetch_object($query))
    $json[] = $result;
print json_encode($json);


I think this is because mysql_fetch_object only returns a single row result. You need something like this:

$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($row = mysql_fetch_array($query))
{
   \\access each result here
}


I can't see how you can have two rows in local

$array = array();
$query = mysql_query('SELECT * FROM `wspm_t_colors`');
while($result = mysql_fetch_object($query))
{
    $array[] = $result;
}
print json_encode($array);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜