简体   繁体   English

使用INNER JOIN打印mySQL查询的结果

[英]Printing result of mySQL query with INNER JOIN

I have a working SQL statement that returned correct results when I checked it on MAMP. 我有一个工作的SQL语句,当我在MAMP上检查它时返回了正确的结果。

SELECT `questions`.`questionID` AS question, `questions`.`questionText`, 
       `questions`.`categoryID`,`answers`.`answerID`,`answers`.`answerText`,
       `answers`.`isTrue`
FROM `questions`,`answers`
WHERE `questions`.`questionID` = `answers`.`questionID`

But I can't figure out how to print the output with php. 但我无法弄清楚如何用PHP打印输出。 Please help. 请帮忙。 This is the code: 这是代码:

<html>
<body>

<?php
    header('Content-Type: text/html; charset=utf-8');
    $con=mysqli_connect("localhost","root","root","Theory");
    // Check connection
    if (mysqli_connect_errno())
    {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    $result = mysqli_query($con,"SELECT `questions`.`questionID` AS question, `questions`.`questionText`, `questions`   .`categoryID`, `answers`.`answerID`,`answers`.`answerText`,`answers`.`isTrue`
                                 FROM `questions`,`answers`
                                 WHERE `questions`.`questionID` = `answers`.`questionID`");

    if (!$result)
    {
        die('Error: ' . mysqli_error($con));
    }
   while($row = mysqli_fetch_array($result))
   {
       echo "{";
       echo "{" . $row['questions'.'questionID'] . "}";   //this is not the full print
       echo "{" . $row['questions'.'questionText'] . "}"; //just for chaking
       echo "}";
   }

    mysqli_close($con);
    ?>

</body>
</head>

I get:"{{}{}}{{}{}}{{}{}}{{}{}}{{}{}}{{}{}}{{}{}}{{}{}}" echoed. 我得到:“{{} {}} {{} {}} {{} {}} {{} {}} {{} {}} {{} {}} {{} {}} {{} { “回声。

You're executing a query again inside your if condition... But the $sql query is empty because the variable is not defined! 您正在if条件中再次执行查询...但$sql查询为空,因为未定义变量!

replace if (!mysqli_query($con,$sql)) with if (!$result) since you have already executed the query in the rows above. 因为你已经在上面的行中执行了查询,所以用if (!$result)替换if (!mysqli_query($con,$sql))

EDIT to answer the question's comments: 编辑回答问题的评论:

when you're fetching the resulting array, you don't need to specify the table alias but just the column name OR the column alias if present. 当您获取结果数组时,您不需要指定表别名,只需指定列名称或列别名(如果存在)。

Try this: 尝试这个:

while($row = mysqli_fetch_array($result))
{
   echo "{";
   echo "{" . $row['questionID'] . "}";   //this is not the full print
   echo "{" . $row['questionText'] . "}"; //just for checking
   echo "}";
}

$sql is aparently not set. $sql显然没有设置。 You can do: 你可以做:

$result = mysqli_query($con,"SELECT `questions`.`questionID` AS question, `questions`.`questionText`, `questions`   .`categoryID`, `answers`.`answerID`,`answers`.`answerText`,`answers`.`isTrue`
                             FROM `questions`,`answers`
                             WHERE `questions`.`questionID` = `answers`.`questionID`");

if (!$result)
{
    die('Error: ' . mysqli_error($con));
}

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

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