【问题标题】:MySQL Select and then echo out resultsMySQL 选择然后回显结果
【发布时间】:2011-05-07 16:36:35
【问题描述】:
$sql_2 = 'SELECT id, firstname, lastname, birthday, location, occupation, telephone, email, picture FROM pinkmoon_pending ORDER BY `id` DESC LIMIT 1';
$result_2 = mysql_query($sql_2);

while ($row = mysql_fetch_row($result_2)
{
   $firstname = row['firstname'];
   $lastname  = row['lastname'];
   $birthday  = row['birthday'];
   $location  = row['location'];
   $occupation= row['occupation'];
   $telephone = row['telephone'];
   $email     = row['email'];
   $picture   = row['picture'];
}
mysql_free_result($result_2);

这是我在使用 PHP 选择行后尝试向我显示的行。

这似乎不起作用 - 任何人都可以帮助我吗? :)

【问题讨论】:

  • 当您将结果限制为最大值时,您不需要执行循环。 1 行,if 就足够了。并在使用关联数组时使用mysql_fetch_assoc

标签: php sql mysql


【解决方案1】:

尝试将mysql_fetch_row 更改为mysql_fetch_assoc。前者仅按数字索引结果,而不是按字段名称。

【讨论】:

    【解决方案2】:

    应该是

    while ($row = mysql_fetch_row($result_2))
    

    你错过了)

    【讨论】:

    • 虽然你是对的,但它仍然不起作用。请参阅 Kaivosukeltaja 的回答。
    【解决方案3】:
    - while ($row = mysql_fetch_row($result_2)
    - {
    -    $firstname = row['firstname'];
    -    $lastname  = row['lastname'];
    -    $birthday  = row['birthday'];
    -    $location  = row['location'];
    -    $occupation= row['occupation'];
    -    $telephone = row['telephone'];
    -    $email     = row['email'];
    -    $picture   = row['picture'];
    - }
    
    + while ($row = mysql_fetch_row($result_2)) {
    +     foreach ($row as $item) {
    +         echo $item;
    +     }
    + }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-26
      • 2011-02-27
      • 2011-03-13
      • 1970-01-01
      相关资源
      最近更新 更多