dongnius85154 2015-05-04 02:30
浏览 39
已采纳

PHP - 数据库列数据查询返回另一列数据

I am writing a basic PHP login system for a school project, I am currently trying to implement email activation. Here is a snippet of my code.

        $query = " 
        SELECT 
            id, 
            username, 
            password, 
            salt, 
            email
            useractivestatus
        FROM users 
        WHERE 
            username = :username 
    "; 

    // The parameter values 
    $parameters = array( 
        ':username' => $_POST['username'] 
    ); 

    try 
    { 
        // Execute the query against the database 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($parameters); 
    } 
    catch(PDOException $err) 
    { 

        die("Failed to run query: " . $err->getMessage()); 
    } 
    $row = $stmt->fetch();
    if($row) 
    {
    if($active != $row['useractivestatus'] );
    {
        die("Account has not been activated");
    }

The problem is using var_dump$row['useractivestatus'] I can see it always returns the data from the email column, causing the query to always return false and trigger the die response. I can do var_dump$row['any other column'] and it returns the correct data. why is the it returning the email when querying the useractivestatus column?

  • 写回答

1条回答 默认 最新

  • doujiao3016 2015-05-04 02:35
    关注

    You are missing a comma in your query, as such email is actually aliased as useractivestatus and trying to select from the email column would fail.

    $query = " 
    SELECT 
        id, 
        username, 
        password, 
        salt, 
        email, -- this comma was missing
        useractivestatus
    FROM users 
    WHERE 
        username = :username 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我知道什么是混合树,但是怎么写代码啊
  • ¥50 开发板linux系统安装dpkg,apt函数库 有偿
  • ¥15 浏览器时间循环 交互事件和延时事件的 优先级与执行问题
  • ¥15 GD模块安装出错,libgd无法正常安装
  • ¥20 求有缘人帮我把笛卡尔坐标系转换为经纬度 有偿
  • ¥15 vue2中使用计算属性
  • ¥50 远程桌面打开Mastercam、没有许可证、物理机打开正常
  • ¥15 ubuntu安装gdal后java读取tif文件报错
  • ¥15 请问lammps怎么做两种金属连接的原子浓度分布图
  • ¥15 求jacquard数据集