【问题标题】:Retrieving data from database and storing in an array从数据库中检索数据并存储在数组中
【发布时间】:2015-04-26 08:13:34
【问题描述】:

我想从 mysql 数据库中检索数据并存储在一个数组中。稍后我会在 html 中显示数据。

下面是我的代码sn-p:

select co_no from faculty_co where course_code='$course1_code';

输出将显示总共 5 个co_no 值。我想将此值存储在一个数组中,并使用 select 标签在 html 中显示为下拉菜单。

我是 php 新手。请告诉我如何检索和存储它。

【问题讨论】:

标签: php mysql


【解决方案1】:

首先,php.net 有很多关于 mysql 和其他东西的文档,而且很容易理解。

查看文档和示例here

【讨论】:

  • 一般来说,推荐一个不推荐使用的 API 被认为是错误的形式。
  • @Strawberry 感谢您指出这一点。我可能应该远离这个。我确定了答案。
【解决方案2】:

我是“老派”,不是一个程序员,所以我倾向于按程序做事。

我这样做是这样的:

include('path/to/connection/stateme.nts'); // the connection is a variable '$db'

$query = "my query";

$result = mysqli_query($db,$query);

$rows = array(); //initialise array

while($row = mysqli_fetch_assoc($result)){
$rows[] = $row;
} 

现在您可以将其存储为 json_encoded 字符串或将其保留为数组以供以后使用。

【讨论】:

    猜你喜欢
    • 2017-05-26
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    相关资源
    最近更新 更多