duangewu5234 2017-04-12 18:47
浏览 26
已采纳

从会话用户名派生的外键ID中获取ID和行

I am trying to get an ID and a row called info to be shown based of the username.

I have a users table:

TABLE users (
id       INTEGER PRIMARY KEY AUTOINCREMENT
                 NOT NULL,
username TEXT    NOT NULL
                 UNIQUE,
password TEXT    NOT NULL

and an infos table that has the userid as an FK:

TABLE infos (
infoid  INTEGER NOT NULL
                  PRIMARY KEY AUTOINCREMENT,
info    TEXT    NOT NULL,
productid INTEGER REFERENCES product (productid) 
                  NOT NULL,
userid    INTEGER REFERENCES users (id) 
                  NOT NULL

I have managed to get the username from the session and the userid:

// get username
$name = $_SESSION['username'];
$stmt = $db->prepare("SELECT id FROM users WHERE username = :name");
$stmt->execute(["name" => $name]);
$r = $stmt->fetch(); 

// get userid
$stmnt2 = $db->prepare("SELECT info FROM infos WHERE userid = :r");
$stmnt2->execute(["r" => $r['id']]);

I am struggling to get all the info id and info based on the userid. I have managed to get the info but it misses out the first entry and I can't get the infoid

    // get info based on userid
    if ($info = $stmnt2->fetch()) {
        echo '<p>Your Info:</p>';   
            while ($info = $stmnt2->fetch()) {
            echo "$info[info] </br>";
            }
   } else {
        echo "<p>No Info</p>";

Can someone tell me how I can get infoid and info to be shown thanks.

  • 写回答

1条回答 默认 最新

  • douyuan1752 2017-04-12 19:11
    关注

    Replace

    // get userid
    $stmnt2 = $db->prepare("SELECT info FROM infos WHERE userid = :r");
    $stmnt2->execute(["r" => $r['id']]);
    

    with

    // get userid
    $stmnt2 = $db->prepare("SELECT info, infoid AS id FROM infos WHERE userid = :r");
    $stmnt2->execute(["r" => $r['id']]);
    

    and replace

    // get info based on userid
    if ($info = $stmnt2->fetch()) {
        echo '<p>Your Info:</p>';   
            while ($info = $stmnt2->fetch()) {
            echo "$info[info] </br>";
            }
    } else {
        echo "<p>No Info</p>";
    }
    

    with

    // get info based on userid
    
    if ($info = $stmnt2->fetch()) {
        echo '<p>Your Info:</p>';   
        do {
            echo "$info[info] </br>"; //The info id is contained in the $info['id']
        } while ($info = $stmnt2->fetch());
    } else {
        echo "<p>No Info</p>";
    }
    

    or

    // get info based on userid
    
    if ($stmnt2->num_rows()) {
        echo '<p>Your Info:</p>';   
        while ($info = $stmnt2->fetch()){
            echo "$info[info] </br>"; //The info id is contained in the $info['id']
        }
    } else {
        echo "<p>No Info</p>";
    }
    

    I would suggest you the second way. Do not hesitate to ask me for any explanation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM