【问题标题】:PHP print from database without 'while'PHP从数据库打印没有'while'
【发布时间】:2011-09-24 23:32:32
【问题描述】:

我需要从表格中打印一行,所以不需要 while 循环,还有其他方法吗?

【问题讨论】:

    标签: php mysql database printing


    【解决方案1】:

    你不需要。

    只需在室外执行 while 条件 1 次即可。

    $a=mysql_fetch_row($sql);
    //use $a
    

    而不是

    while($a=mysql_fetch_row($sql)){
        //use $a
    }
    

    【讨论】:

    • 虽然你应该使用 PDO。
    【解决方案2】:
    if (($dbResult = mysql_query("SELECT ... FROM ... LIMIT 1")) !== false)
    {
      $row = mysql_fetch_array($dbResult);
      echo $row['Column_Name'];
    }
    

    只需获取一行,无需总是循环检索。

    【讨论】:

      【解决方案3】:
      $results = mysql_query("SELECT * FROM my_table WHERE user_id = 1234");
      $row = mysql_fetch_assoc($results);
      echo ($row['user_id']);
      

      【讨论】:

        【解决方案4】:

        在循环的条件下做你会做的事情,你会没事的。

        【讨论】:

          猜你喜欢
          • 2016-03-24
          • 1970-01-01
          • 2021-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多