MySQL查询仅获取第一行而不是表中的所有行

时间:2013-11-20 09:32:22

标签: php mysql

我试图在MySQL数据库中获取多行,但它只获取第一个条目!

$data = mysql_query("SELECT * FROM twitter_tbl") 
while($info = mysql_fetch_array($data)) 
    { 
            $desc = $info['description'];
            $screen_name1 = $info['screen_name'];
            $final_oauth_token = $info['final_oauth_token'];
            $final_oauth_token_secret = $info['final_oauth_token_secret'];

    );

所以我只得到一行,但我需要获得多行,因为每一行都有其唯一的screen_name和描述。

我不知道自己哪里出错了。

4 个答案:

答案 0 :(得分:2)

更正此代码:

$data = mysql_query("SELECT * FROM twitter_tbl"); 
while($info = mysql_fetch_array($data)) 
    { 
           echo $desc = $info['description'];
           echo $screen_name1 = $info['screen_name'];
          echo  $final_oauth_token = $info['final_oauth_token'];
          echo  $final_oauth_token_secret = $info['final_oauth_token_secret'];

    }

答案 1 :(得分:2)

虽然语句包含在{}而不是{);

编辑:

$data = mysql_query("SELECT * FROM twitter_tbl") 
while($info = mysql_fetch_array($data)) 
{ 
        Echo $desc = $info['description'];
        Echo $screen_name1 = $info['screen_name'];
        Echo $final_oauth_token = $info['final_oauth_token'];
        Echo $final_oauth_token_secret = $info['final_oauth_token_secret'];

}

答案 2 :(得分:1)

您只需将其放在一个可在您的网页上显示结果的容器中

试试这个:

       echo "<table>";

       $data = mysql_query("SELECT * FROM twitter_tbl");

       while($info = mysql_fetch_array($data)) 
       { 
          $desc = $info['description'];
          $screen_name1 = $info['screen_name'];
          $final_oauth_token = $info['final_oauth_token'];
          $final_oauth_token_secret = $info['final_oauth_token_secret'];

         echo "
         <tr>
            <td>".$desc."</td>
            <td>".$screen_name1."</td> 
            <td>".$final_oauth_token."</td>
            <td>".$final_oauth_token_secret."</td>
         </tr>";

         }
         echo "</table>";

答案 3 :(得分:0)

将数据添加到数组中。 你的目标是什么? (存储数据?echo?)

CODE:

$data = mysql_query("SELECT * FROM twitter_tbl");


if($data && mysql_num_rows($data) > 0)
{
    while($info = mysql_fetch_array($data)) 
    { 
            $desc[$i] = $info['description'];
            $screen_name1[$i] = $info['screen_name'];
            $final_oauth_token[$i] = $info['final_oauth_token'];
            $final_oauth_token_secret[$i] = $info['final_oauth_token_secret'];

        ++$i;

    }

    unset($i, $info, $data);
}

或者喜欢Echo模式

CODE:

$data = mysql_query("SELECT * FROM twitter_tbl");


if($data && mysql_num_rows($data) > 0)
{

    $x = "
<table>
   <tr>
      <th>
        desc
      </th>
      <th>
        screen_name
      </th>
      <th>
        oauth Token
      </th>
      <th>
        secret oauth Token
      </th>
   </tr>";


    //Warning it's eat a lot of memory
    while($info = mysql_fetch_array($data)) 
    { 


            $x .= "
   <tr>
      <td>
        {$info['description']}
      </td>
      <td>
        {$info['screen_name']}
      </td>
      <td>
        {$info['final_oauth_token']}
      </td>
      <td>
        {$info['final_oauth_token_secret']}
      </td>
   </tr>";

    }

    $x = "
</table>";

    echo $x;

    unset($info, $data, $x);
}