【问题标题】:Select all from the db从数据库中选择所有
【发布时间】:2015-03-16 22:01:50
【问题描述】:

我需要打印数据库中的所有用户,但我该怎么做呢?我无法绑定baram,因为没有要绑定的东西?

这是我的代码:

session_start();
    require 'inc/connect.php';

$hey = $mysqli->prepare("SELECT * FROM user");
$hey->execute();
$hey->bind_result($all);
$hey->fetch();
$hey->close();

echo $all;

【问题讨论】:

  • 你必须绑定吗?
  • user 表中有多少列?向bind_result 提供这么多变量。正如你所拥有的那样,$all 被分配了第一列值。
  • @CargoMeister $hey->bind_param(我在这里输入什么?);
  • @bloodyKnuckles 有七个,但我如何绑定所有这些?
  • @bloodyKnuckles 但我收到此消息:警告:mysqli_stmt::bind_result():绑定变量的数量与 C:\xampp\htdocs\admin.php 中准备好的语句中的字段数不匹配7号线

标签: select mysqli bind


【解决方案1】:

如果表user有七列,给bind_result七个变量名:

$hey->bind_result($column_one, $column_two, $column_three, $column_four, $column_five, $column_six, $column_seven);

当然,使用反映所代表数据性质的变量名称。

现在大家一起:

session_start();
    require 'inc/connect.php';

$hey = $mysqli->prepare("SELECT * FROM user");
$hey->execute();
$hey->bind_result($id, $fname, $lname, $email, $phone, $addy, $age);
while ( $hey->fetch() ) {
  echo "$id $fname $lname $email $phone $addy $age<br>";
}
$hey->close();

我更喜欢明确地使用 SQL 并在结果中调用我想要的列:

SELECT id, fname, lname, email, phone, addy, age FROM user

...为了保护我的查询,以防以后将案例列添加到表中。

【讨论】:

  • 我该如何回应它们?
  • 我添加了一个示例,如何循环遍历 fetchecho 每条记录。
  • 警告:mysqli_stmt::bind_result():绑定变量的数量与第 7 行 C:\xampp\htdocs\admin.php 中准备好的语句中的字段数不匹配
  • 我的变量名有错字。修好了。
  • 我已经有 10 个了,现在可以使用了'不知道我如何为列表中的每个人制作一个按钮..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-23
  • 1970-01-01
  • 2015-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多