简体   繁体   English

为什么我没有在查询代码中得到所有结果?

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

I am having 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. 下面的代码在我的本地计算机上工作正常,但是在实时服务器上,它不能正常工作..我应该得到两行,但是在实时服务器上,我只得到1个结果。

$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. mysql_fetch_object / array / row总是只返回一行并将指针移到下一行,如果没有下一行则返回false。

I think this is because mysql_fetch_object only returns a single row result. 我认为这是因为mysql_fetch_object仅返回单行结果。 You need something like this: 您需要这样的东西:

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

Your code is only getting one row. 您的代码仅获得一行。 The mysql_fetch_object() function only returns one row. mysql_fetch_object()函数仅返回一行。 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 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM