gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:47:12 25 4
gpt4 key购买 nike

我有一个有效的 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`

但我不知道如何用 php 打印输出。请帮忙。这是代码:

<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>

我得到:“{{}{}}{{}{}}{{}{}}{{}{}}{{}{}}{{}{}}{{}{}}{ {}{}}”回应道。

最佳答案

您在 if 条件中再次执行查询...但是 $sql 查询为空,因为变量未定义!

if (!mysqli_query($con,$sql)) 替换为 if (!$result) 因为您已经在上面的行中执行了查询。

编辑以回答问题的评论:

当您获取结果数组时,您不需要指定表别名,只需指定列名或列别名(如果存在)。

试试这个:

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

关于php - 使用 INNER JOIN 打印 mySQL 查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17657797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com