dqknycyt92288 2013-09-16 21:18
浏览 14
已采纳

同时使用两个查询。 PDO

Okay so, I don't really know anything about PDO, my friend just asked me to post this here since he's not very good at English. Anyway, this is how he explained it to me:

The code provided is supposed to get a couple of values, save them, and it's supposed to get something out of another table with the help of the values gotten from earlier. The problem according to my friend is that it doesn't get the second value.

Code:

$user_email = $_SESSION['user_email'];
$query = $db->prepare("SELECT username,id,password FROM user WHERE email=:email");
$query->bindParam(':email', $user_email, PDO::PARAM_INT);
$query->execute();
$row = $query->fetch();
$user_username=$row['username'];
$user_group=$row['group'];   
$query_group = $db->prepare("SELECT color,name FROM group WHERE id=:id");
$query_group->bindParam(':id', $user_group, PDO::PARAM_INT);
$query_group->execute();
$row = $query_group->fetch();
$group_color=$row['color'];
$group_name=$row['name'];
  • 写回答

2条回答 默认 最新

  • douyuanliao8815 2013-09-16 21:21
    关注

    The word group used as a table name needs to be enclosed in backticks. group is a reserved key word (GROUP BY clause).

    SELECT 
        color, 
        name 
    FROM `group`
    WHERE id = :id
    

    Using the above would work.


    You can shorten the entire code by using a JOIN clause too. As commented above by Prix, the code shall be:

    $user_email = $_SESSION['user_email'];
    $query = $db->prepare("SELECT 
        u.username,
        u.id,
        u.password,
        g.color,
        g.name
    FROM user u
    JOIN `group` g
        ON g.id = u.id
    WHERE u.email = :email");
    // I think emails are supposed to be `PDO::PARAM_STR`
    $query->bindParam(':email', $user_email, PDO::PARAM_INT);
    $query->execute();
    $row = $query->fetch();
    $user_username = $row['username'];
    $group_color = $row['color'];
    $group_name = $row['name'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号