简体   繁体   English

提取数据错误关系表

[英]Fetching data error relational tables

I am trying to retrieve some data from my database where I have a join in place. 我试图从我已有连接的数据库中检索一些数据。 I have 3 tables: 我有3张桌子:

users column=user_id cats column=cat_id user_cat_join columns-user_is & cat_id 用户列=用户ID猫列=猫ID用户_加入列-用户ID和猫ID

The user_cat_join table are related to to the corresponding tables. user_cat_join表与相应的表相关。

I am trying to get the data out the table to only show the categories assigned to the user I am logged in as. 我试图从表中获取数据,以仅显示分配给我作为登录用户的类别。 So, I am getting the users ID from the session function and then trying to draw out the data I require. 因此,我要从会话功能获取用户ID,然后尝试提取所需的数据。

require ('../db_con.php'); 要求('../db_con.php');

// BUILD AND DISPLAY THE CATEGORY LIST (RICOH BUILD)

function build_cat_list()
{

    global $dbc;

    // DEFINE THE QUERY:
    $user = $_SESSION['user'];
    $q = "SELECT cat_name, cat_icon, cat_color
                                FROM cats
                                INNER JOIN user_cat_join
                                ON cats.cat_id = user_cat_join.cat_id
                                WHERE user_cat_join.user_id = $user"; 
    $r = mysqli_query ($dbc, $q); // Run the query.


    // FETCH AND PRINT ALL THE RECORDS
    while ($row = mysqli_fetch_array($r)) {
    echo '

    <a href="view_cat.php?cat_id='.$row["cat_id"].'">
        <div class="indexBox"">
            <div class="indexBoxHeader"  style="background-color:'.$row["cat_color"].'"><p>'
                .$row["cat_icon"].'</p>
                </div>
            <div class="indexBoxFooter">
                <p>'.$row["cat_name"].'</p>
            </div>
        </div>
    </a>';

    }
}

when displaying this I get the following error? 显示此错误时出现以下错误?

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\MAMP\htdocs\home.php on line 33

The above is referencing this: 上面引用了这个:

while ($row = mysqli_fetch_array($r)) {

I have done somre further error reporting and by doing this: 我做了进一步的错误报告,并这样做:

if($r === FALSE) { 
     printf(mysqli_error($dbc));
    exit();
}  

I get this: 我得到这个:

Unknown column '5ad3e66c5e1e7747707c480dca04fc85' in 'where clause' “ where子句”中的未知列“ 5ad3e66c5e1e7747707c480dca04fc85”

I have no idea where that is referring to? 我不知道这是在指什么?

UPDATE 更新

<?php
    error_reporting(E_ALL);
    ini_set('display_errors', '1');

    // BUILD AND DISPLAY THE CATEGORY LIST (RICOH BUILD)

    global $dbc;

    // DEFINE THE QUERY:
    $user = $_SESSION['user'];

    $q = sprintf("
        SELECT
            cat_name, cat_icon, cat_color
        FROM
            cats
        INNER JOIN
            user_cat_join
        ON
            cats.cat_id = user_cat_join.cat_id
        WHERE
            user_cat_join.user_id = '%s'
        ",
        mysqli_real_escape_string($dbc, $user)
        );


     $r = mysqli_query ($dbc, $q); // Run the query.

    if($r === FALSE) { 
        printf(mysqli_error($dbc));

    }  


    // FETCH AND PRINT ALL THE RECORDS
    while ($row = mysqli_fetch_array($r)) {

       var_dump($row);

    echo '
    <a href="view_cat.php?cat_id='.$row["cat_id"].'">
        <div class="indexBox"">
            <div class="indexBoxHeader"  style="background-color:'.$row["cat_color"].'"><p>'
                .$row["cat_icon"].'</p>
                </div>
            <div class="indexBoxFooter">
                <p>'.$row["cat_name"].'</p>
            </div>
        </div>
    </a>';

    }
?>

You have to quote your user variable: 您必须引用您的用户变量:

$q = "SELECT cat_name, cat_icon, cat_color
                            FROM cats
                            INNER JOIN user_cat_join
                            ON cats.cat_id = user_cat_join.cat_id
                            WHERE user_cat_join.user_id = '$user'"; 

Shouldn't your WHERE be like below, otherwise your current WHERE clause looks like WHERE user_cat_join.user_id = 5ad3e66c5e1e7747707c480dca04fc85 ; 您的WHERE应该像下面这样,否则您当前的WHERE子句看起来像WHERE user_cat_join.user_id = 5ad3e66c5e1e7747707c480dca04fc85 ; which your DB engine thinking is a separate column and so complaining about the same. 您的数据库引擎认为这是一个单独的专栏,因此对此有所抱怨。

WHERE user_cat_join.user_id ='". $user. "'"

In short it should looks like 总之应该看起来像

$q = "SELECT cat_name, cat_icon, cat_color
                            FROM cats
                            INNER JOIN user_cat_join
                            ON cats.cat_id = user_cat_join.cat_id
                            WHERE user_cat_join.user_id ='". $user. "'";

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

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