简体   繁体   English

使用JOIN未成功使用PHP查询MySQL数据库

[英]Unsuccessfully using JOINs to query MySQL database with PHP

Here is my database schema: 这是我的数据库架构:

user
*user_id
*username
*password
*etc

quiz_response
*response_id
*user_id
*question_id
*response
*is_correct
*answer_time

question_choice
*choice_id
*question_id
*is_correct
*choice (VARCHAR)

question
*question_id
*quiz_id
*question (VARCHAR)

quiz
*quiz_id
*title (VARCHAR)

I am building a quiz web-app using PHP and I am having trouble. 我正在使用PHP构建测验网络应用程序,我遇到了麻烦。 Currently, I am trying -- with no luck -- this query and I know where the problem is, I just don't know how to solve it. 目前,我正在尝试 - 没有运气 - 这个查询,我知道问题在哪里,我只是不知道如何解决它。 Hence, why I am here on SO 因此,为什么我在这里

// Grab the response data from the database to generate the form
    $query = "SELECT qr.response_id AS r_id, qr.question_id, qr.response, q.question, quiz.title " . 
        "FROM quiz_response AS qr " . 
        "INNER JOIN question AS q USING (question_id) " . 
        "INNER JOIN quiz USING (quiz_id) " . 
        "WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
    $data = mysqli_query($dbc, $query) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query"); 

At this point I feel my second Inner Join (INNER JOIN quiz USING (quiz_id)) is the problem. 在这一点上,我觉得我的第二个内部加入(INNER JOIN测验使用(quiz_id))是问题所在。 When I don't include this line and remove the quiz.title from the query it works. 当我不包含此行并从查询中删除quiz.title时它可以工作。 So, my question is how do I maintain an atomic database schema while still grabbing the quiz title based on the quiz_id from the table 'question'? 所以,我的问题是如何在仍然根据表'问题'中的quiz_id获取测验标题的同时维护原子数据库模式? Any help would be much appreciated! 任何帮助将非常感激!

Try with: 试试:

// Grab the response data from the database to generate the form
    $query = "SELECT qr.response_id AS r_id, qr.question_id, qr.response, q.question, quiz.title " . 
        "FROM quiz_response qr " . 
        "INNER JOIN question q USING (question_id) " . 
        "INNER JOIN quiz ON quiz.quiz_id = q.quiz_id " . 
        "WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
    $data = mysqli_query($dbc, $query) or die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query"); 

I believe that the problem is that quiz_id is not in quiz_response. 我认为问题是quiz_id不在quiz_response中。 I use the ON keyword. 我使用ON关键字。 Try: 尝试:

// Grab the response data from the database to generate the form
$query = "SELECT qr.response_id AS r_id, qr.question_id, qr.response, q.question, 
          quiz.title " . 
         "FROM quiz_response AS qr " . 
         "INNER JOIN question AS q ON (q.question_id = qr.question_id) " . 
         "INNER JOIN quiz ON (quiz.quiz_id = q.quiz_id) " . 
         "WHERE qr.user_id = '" . $_SESSION['user_id'] . "'";
$data = mysqli_query($dbc, $query) or 
         die("MySQL error: " . mysqli_error($dbc) . "<hr>\nQuery: $query");

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

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