【问题标题】:This code always return $id but not $first_name [duplicate]此代码始终返回 $id 但不返回 $first_name [重复]
【发布时间】:2016-12-28 00:01:35
【问题描述】:

我正在使用 PDO+Mysql 从会话中传递 udid,如果行数大于 0,我想从表中获取 id 以及 first_name,即 fifth表中的列。

问题是 id 总是正确返回,但不是 first_name,不知道为什么

$stmt = $con->prepare("SELECT * FROM members WHERE id= :udid");                     
$stmt->bindValue(':udid', $_SESSION['udid']);
$stmt->execute();

$id=$stmt->fetchColumn();
$first_name=$stmt->fetchColumn(5);

【问题讨论】:

  • 你应该真正命名你想要选择的列,而不是使用select *
  • @juergend 我想返回 10 列中的近 9 列
  • 将行作为 assoc 数组获取并使用字段名称访问它有什么问题?为什么需要使用数字?
  • 使用 $row = $stmt->fetch();它返回整行。 $first_name = $row['first_name']
  • 单独选择列比使用* 选择所有列更快。

标签: php mysql pdo


【解决方案1】:

我认为您可以使用fetch() 获取整行,然后按其名称调用每一列

$stmt->execute();
$result = $stmt->fetch();
$id = $result['id'];
$name = $result['name'];
//and so on for all the columns

【讨论】:

  • @yourcommonsense 关心解释否决票?或者只是......它是错误的、错误的、失败的或其他的
  • @yourcommonsense 只是来投反对票,看来
  • 是的。它被称为“挫折”。不过没什么私人的。就在您浪费生命寻找副本时,有人偷偷溜进来回答一个愚蠢的问题。但我必须祝贺你——你终于写出了一个没有错的答案。
  • @YourCommonSense 哈哈很有趣
猜你喜欢
  • 2019-06-08
  • 2021-07-11
  • 2014-07-07
  • 2021-03-05
  • 2018-09-22
  • 2020-11-04
  • 1970-01-01
  • 2021-07-16
  • 1970-01-01
相关资源
最近更新 更多