简体   繁体   English

如何从MySQL中的索引字段输出另一个表的值?

[英]How to output a value of an other table from an indexed field in mySQL?

I have two MySQL tables 我有两个MySQL表

  1. users with id, user_formOfAdress and serveral additional fields. ID,user_formOfAdress和服务器附加字段的用户。 Field user_formOfAdress contains the id of table formofadress 字段user_formOfAdress包含表formofadress的ID

  2. formofadress with id, formOfAdress_german and serveral additional fields, for example id 1 = Mr., id 2 = Mrs. 带有id,formOfAdress_german和服务器附加字段的形式表,例如id 1 = Mr.,id 2 = Mrs.

The record of the users table is identified by a Session variable. 用户表的记录由会话变量标识。

To output not the id of the field user_formOfAdress but the value of the table formofadress.formOfAdress_german (for example Mr. or Mrs.) I have written this: 要输出的不是字段user_formOfAdress的ID,而是要输出表formofadress.formOfAdress_german的值(例如Mr.或Mrs.),我已编写了此代码:

if(array_key_exists("id", $_SESSION) && $_SESSION['id']){
        $uid = $_SESSION['id'];
        $link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
        $query = "
        SELECT formofadress.ID AS formofadress_ID, formofadress.formOfAdress_german, users.ID, users.user_formOfAdress 
        FROM `formofadress` 
        LEFT JOIN users 
        ON formofadress.formofadress_ID = users.user_formOfAdress
        WHERE `users.ID` = '".$uid."' 
        LIMIT 1
        ";
        $result = mysqli_query($link, $query);
        $record = mysqli_fetch_assoc($result); 
    $user_formOfAdress = $record['formOfAdress_german'];
        }

"FROM formofadress" because I want to output the Mr. or Mrs. of this table, but I have to use also the users table because of the Session ID which is also the id of the users record ... “ FROM formofadress”是因为我想输出此表的先生或太太,但是由于会话ID(这也是用户记录的ID),我还必须使用users表。

Not every record in the users table has a value in user_formOfdAdress (value 1, 2 or NULL) but every record in the formofadress table has a fixed value. 并非用户表中的每个记录都具有user_formOfdAdress中的值(值1、2或NULL),但是forfafadress表中的每个记录都具有固定值。

Error is: Undefined variable: user_formOfAdress located in the last row 错误是:未定义的变量:user_formOfAdress位于最后一行

It's my first time to use JOINs and I'm unfortunately not able to solve this issue even after a long time of searching. 这是我第一次使用JOIN,很遗憾,即使经过长时间的搜索,我仍然无法解决此问题。

Correct code: 正确的代码:

if(array_key_exists("id", $_SESSION) && $_SESSION['id']){
        $uid = $_SESSION['id'];
        $link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
        $query = "
            SELECT formofadress.ID, formofadress.formOfAdress_german, users.ID, users.user_formOfAdress 
            FROM formofadress 
            LEFT JOIN users 
            ON formofadress.ID = users.user_formOfAdress 
            WHERE users.ID = '".$uid."' 
        ";
        $result = mysqli_query($link, $query);
        $record = mysqli_fetch_assoc($result); 
        $user_formOfAdress = $record['formOfAdress_german'];
    }

There were problems with your SQL syntax. 您的SQL语法有问题。 You used LEFT JOIN to join users to formofadress while users sometimes can't follow and you will possibly get a NULL value. 您使用LEFT JOIN加入usersformofadressusers有时无法跟随,你很可能会得到一个NULL值。 I also cleaned up your other query syntax so it is more readable. 我还清理了您的其他查询语法,使它更具可读性。

Also, check to see if the database is expecting an INT for $uid . 另外,检查数据库是否期望$uidINT值。 If not then return the apostrophes '' . 如果不是,则返回撇号''

if(array_key_exists("id", $_SESSION) && $_SESSION['id'])
{
    $uid = $_SESSION['id'];
    $link = mysqli_connect("localhost:3307", "root", "Dinah123", "proficrm");
    $query = "SELECT u.ID, u.user_formOfAdress, f.ID, f.formOfAdress_german,  
    FROM users u LEFT JOIN formofadress f ON f.formofadress_ID = u.user_formOfAdress 
    WHERE u.ID = ".$uid." LIMIT 1";
    // if database is not expecting `INT` for `u.ID` then return the apostrophes ' '

    $result = mysqli_query($link, $query);

    // mysqli_fetch_assoc returns case sensitive keys
    $record = mysqli_fetch_assoc($result);

    $user_formOfAdress = $record['formOfAdress_german'];
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM