0

我一直在互联网上寻找解决我问题的方法。我正在自学关系数据库,并试图在我的网页上回应这一点。但它没有回响:(

请一双新鲜的眼睛看看我的代码并指出我正确的方向。非常感谢

我一直在尝试在出于自己的目的进行编辑之前进行测试运行

database: fault
table: user
collumns: id name course 
foreign key(fk_course)fault,course,id
table: course
Collumns: id coursename

代码:

<?php
require 'connection.php';

//where statement in the sql syntax will select where in db to get infor, use AND to add another condition
$result = mysqli_query($con,"SELECT user.name, course.coursename FROM 'user' INNER JOIN 'course' ON user.course = coursename.id"); //this creates a variable that selects the database

echo "<table border='1'>
<tr>
<th>Name</th>
<th>Course</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>" . $row['name'] . "</td>";
  echo "<td>" . $row['coursename'] . "</td>";
  echo "</tr>";
}
echo "</table>";
?>
4

3 回答 3

1

您的 SQL 语句应如下所示:

SELECT user.name,
       course.coursename
  FROM user INNER JOIN course ON user.course = course.id;

对字符串使用普通的撇号,并且该ON部分应正确匹配字段。

于 2013-08-06T14:07:33.283 回答
1

你写成

SELECT user.name, course.coursename FROM 'user' INNER JOIN 'course' ON user.course = coursename.id

这里的表当然不是课程名称。试试这个

SELECT user.name, course.coursename FROM user INNER JOIN course ON user.course = course.id
于 2013-08-06T14:07:51.547 回答
0

试试这个:

 <?php
 require 'connection.php';

 //where statement in the sql syntax will select where in db to get infor, use AND to add another condition

$result = mysqli_query($con,"SELECT user.name, course.coursename FROM 'user' INNER JOIN 'course' ON user.course = coursename.id"); //this creates a variable that selects the database

echo "<table border='1'> 
<tr> 
<th>Name</th> 
<th>Course</th>  
</tr>";  
while ($row = mysqli_fetch_array($result)) { 
   echo "<tr>"; 
   echo "<td>"<?=$row['name'];?>"</td>";  
   echo "<td>" <?=$row['coursename'];?>"</td>"; 
   echo "</tr>";
}  
echo "</table>";
?> `
于 2013-08-06T14:18:23.347 回答