【问题标题】:Fetch multiple row output from MySQL从 MySQL 获取多行输出
【发布时间】:2013-02-24 18:28:26
【问题描述】:

我想获取多行输出,我知道可以使用 while 循环轻松完成,但我的问题是,如果我使用 while 循环,相应行的标题就会出现。我只是不想更改标题,但想更改内容。这是我想要的简单数据

    tableA

    id                title                            msg

    1                 eeee                              frrffrfrrfrr
    2                 vvfdfd                           ffvfvdfvddd
    3                 vffdcadvg                         fdfvddfgvre
    4                 dfdf                              fvvvf

因此,如果我将使用 while 循环,那么该行的标题也会出现,所以我只想不断更改 row['message'] 值。我的简单 MySQL 查询是

   $res = mysql_query("select * from tableA");
   $row = mysql_fetch_array($res);
    $msg=$row['msg']

如您所见,它会很容易地显示相应行的标题和消息,但我想要一行的标题和 4 行的消息输出如何完成?

【问题讨论】:

  • 所以你想要第一行的 msg 和 title 而后续行的只有 msg?不知道我是否完全理解。
  • mysql_* 已弃用,避免使用它们

标签: php mysql


【解决方案1】:

使用循环:

$res = mysql_query("select * from tableA");
while ($row = mysql_fetch_array($res)) {
    echo $row['msg'];
}

Please, don't use mysql_* functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解 prepared statements,并使用 PDOMySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial

【讨论】:

  • 为什么你使用mysql_fetch_array而不是mysql_fetch_assoc
猜你喜欢
  • 1970-01-01
  • 2021-10-04
  • 2019-11-09
  • 1970-01-01
  • 2012-06-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-30
相关资源
最近更新 更多