php mysql inner join,php – 使用INNER JOIN打印mySQL查询的结果

我有一个工作的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打印输出.请帮忙.这是代码:

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

?>

我得到:“{{} {}} {{} {}} {{} {}} {{} {}} {{} {}} {{} {}} {{} {}} {{} { “回声.

解决方法:

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

因为你已经在上面的行中执行了查询,所以用if(!$result)替换if(!mysqli_query($con,$sql)).

编辑回答问题的评论:

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

试试这个:

while($row = mysqli_fetch_array($result))

{

echo "{";

echo "{" . $row['questionID'] . "}"; //this is not the full print

echo "{" . $row['questionText'] . "}"; //just for checking

echo "}";

}

标签:php,mysql

来源: https://codeday.me/bug/20190715/1468143.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值