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 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含