dongyouzhi7218
2014-08-14 11:44
浏览 88
已采纳

SELECT * FROM没有在MySQL表中获取第一行

I'm not certain if this is a bug or a stupid error on my part, but I'd like to display a list of plays and theatrical performances from a database.

I can get it to display from row/entry 2 downwards fine - but it won't display the first row at all. Where there is only one entry, nothing is displayed. I've tried altering the id's of each, but it seems to just be the first one entered that isn't picked up, but ones entered after are.

The code to display the table is below.

<?php
$con = mysql_connect("localhost"," me "," mypass word");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

  mysql_select_db(" database name", $con);

  $data = mysql_query("SELECT * FROM table") 
 or die(mysql_error());
 $info = mysql_fetch_array( $data );

 while($info = mysql_fetch_array( $data )) 
 {
 Print "<tr><td style='background-color: #D3D3D3;  '>";
 Print " ".$info['title'] . " ";
 Print "</td>";

...etc...

   Print "</td> </tr>";
 } 

?>
    </table>

It seems quite simple but I can't figure it out at all. Any help much appreciated.

图片转代码服务由CSDN问答提供 功能建议

我不确定这是我的错误还是愚蠢的错误,但我想 显示数据库中的剧本和戏剧表演列表。

我可以从行/条目2向下显示它 - 但它根本不会显示第一行。 只有一个条目,不显示任何内容。 我已经尝试改变每个的id,但它似乎只是第一个输入的未被拾取,但是之后输入的是。

显示该表的代码如下所示。

 &lt;?php 
 $ con = mysql_connect(“localhost”,“  me“,”mypass word“); 
if(!$ con)
 {
 die('无法连接:'。mysql_error()); 
} 
 
 mysql_select_db(”数据库名称“,$  con); 
 
 $ data = mysql_query(“SELECT * FROM table”)
或die(mysql_error()); 
 $ info = mysql_fetch_array($ data); 
 
 while($ info = mysql_fetch_array  ($ data))
 {
打印“&lt; tr&gt;&lt; td style ='background-color:#D3D3D3;'&gt;”; 
打印“”。$ info ['title']。  “”; 
打印“&lt; / td&gt;”; 
   
 
 

...等...

 < 代码>打印“&lt; / td&gt;&lt; / tr&gt;”; 
} 
 
?&gt; 
&lt; / table&gt; 
   
 
 

看起来很简单,但我根本无法解决。 任何帮助非常感谢。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • dongyi0210 2014-08-14 11:45
    已采纳

    remove this line

     $info = mysql_fetch_array( $data );
    

    you are using it twice. above and in while loop

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doujiao1814 2014-08-14 11:44

    You are calling mysql_fetch_array() twice.

    Remove the first:

     $info = mysql_fetch_array( $data );
    
    评论
    解决 无用
    打赏 举报
  • douyue7536 2014-08-14 11:46

    remove this line

     $info = mysql_fetch_array( $data );
    

    Try this

    <?php
    $con = mysql_connect("localhost"," me "," mypass word");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
      mysql_select_db(" database name", $con);
    
      $data = mysql_query("SELECT * FROM table") 
     or die(mysql_error());
    
     while($info = mysql_fetch_array( $data )) 
     {
     Print "<tr><td style='background-color: #D3D3D3;  '>";
     Print " ".$info['title'] . " ";
     Print "</td>";
    
    评论
    解决 无用
    打赏 举报
  • douzhanshen0657 2014-08-14 11:55

    You are calling the mysql_fetch_array() two times - in the while loop and above it.

     $info = mysql_fetch_array( $data );
    
    评论
    解决 无用
    打赏 举报
  • dongshengyin0147 2014-08-14 11:58

    It because, you are fetching the array twice:

    $info = mysql_fetch_array( $data );

    Here your arrays current first element is fetched and current element is set to second one. So the following line fetches the array elements from the second element.

    while($info = mysql_fetch_array( $data ))

    Just remove the line

    $info = mysql_fetch_array( $data );

    And done.

    评论
    解决 无用
    打赏 举报
  • dongmopu6734 2014-08-14 12:34

    It ain't a bug.

    Whenever you send mysql_fetch_array( $data ); it fetches one row and automatically rises the internal row identifier property. The next time when you call the same function it starts from the second row.

    It is unpractical to use assignment in conjunction with PHP mysql functions outside a loop, when demonstrated behavior is not exactly what you want to do.

    By removing the $info = mysql_fetch_array( $data ); row your code will function as expected.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题