1

我希望这是一个简单的编码错误。当试图从数据库表中获取所有结果时,总是会错过第一个结果。即使在数据库中添加和删除信息时,它也总是会丢失 ID 最低的结果。

$data = mysql_query("SELECT * FROM tbl_messages") 
 or die(mysql_error()); 

 $info = mysql_fetch_array( $data ); 

 while($info = mysql_fetch_array( $data )) 
 { 
 Print "<a href=\"../message/index.php?user=\">" .$info['subject']. "</a>";
  echo "<hr>";
 } 
4

4 回答 4

2

您当前的代码调用mysql_fetch_array并将结果(结果集的第一行)分配给$info. 你不做任何事情$info,在下一行你用另一个调用覆盖它mysql_fetch_array

您需要删除此行:

$info = mysql_fetch_array( $data );
于 2013-06-27T11:00:41.893 回答
1

From the php manual:

array mysql_fetch_array ( resource $result [, int $result_type = MYSQL_BOTH ] )  

Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.

So after $data = mysql_query(...) you get an array $data and the internal data pointer points on the first element of $data.

The first call of mysql_fetch_array(...) moves the internal data point ahead, so it now points to the second element.

Then, your while-loop calls mysql_fetch_array(...) AGAIN before issuing the first Print, so the second element gets printed.

You can fix it by removing this line:

$info = mysql_fetch_array( $data ); 
于 2013-06-27T11:12:00.647 回答
0

删除第一个mysql_fetch_array语句。

就像是

$data = mysql_query("SELECT * FROM tbl_messages") 
 or die(mysql_error()); 

 while($info = mysql_fetch_array( $data )) 
 { 
 Print "<a href=\"../message/index.php?user=\">" .$info['subject']. "</a>";
  echo "<hr>";
 } 

您正在使用上面的代码跳过第一个条目。

mysql_fetch_array

返回与获取的行对应的数组,并将内部数据指针向前移动。

Which means that when you get to the while, you have already read the first row, but not used it yet.

于 2013-06-27T11:01:20.567 回答
0

I had problems like this too with my table query's ignoring row 0 (the first occurance) and now that I see this I finally understand what's going on.

The clarification is also understandable.

You are asking for a query_fetch_array and you put what it finds in $info. This seems ok. Next you ask for the remaining rows, since you just removed one row from the possible returns, it also works perfectly.

Remove this line:

$info = mysql_fetch_array( $data );

and you get all your rows from your table. Or, if you are wondering, ECHO the $info before your while loop in the fashion you are printing or just try var_dumping it.

于 2016-07-15T18:13:39.547 回答