dongxin991209 2013-04-25 23:22
浏览 44
已采纳

此PDO语句返回整数而不是字符串

In a class, I have some PDO:

$userFName = 'userFName';
include('dbconnect.php');       // Normally I'd store the db connect script outside of webroot
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
$stmt = $pdo->prepare('SELECT userFName FROM Users WHERE username = :uname AND password = :pword AND roleID = 1');
$stmt->bindParam(':uname', $this->user->username);
$stmt->bindParam(':pword', $this->user->password);
$stmt->bindColumn(4, $userFName, PDO::PARAM_STR);
$stmt->execute();
$familiar = $stmt->fetch(PDO::FETCH_BOUND);
$this->user->firstName = $familiar;

It's returning the ID in the first column instead of the VARCHAR contents in the 4th column. Any idea why?

  • 写回答

2条回答 默认 最新

  • dongmie3526 2013-04-25 23:26
    关注

    When using PDO::FETCH_BOUND with fetch(), the method will not return a result record. Instead the value of the column should be available in the variable you have bound using $stmt->bindColumn() earlier.

    So change your code to:

    $stmt->bindColumn(1, $userFName, PDO::PARAM_STR);
    $stmt->execute();
    $stmt->fetch(PDO::FETCH_BOUND);
    $this->user->firstName = $userFName; // <-- use the bound variable
    

    However you won't need that bindColumn() call. You could simplify the code as this:

    $stmt->execute();
    $row = $stmt->fetch(); // uses PDO::FETCH_ASSOC by default
    $this->user->firstName = $row['FName'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效