php echo并加入mysql数据库

时间:2013-08-06 13:59:42

标签: php mysql

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

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

为了我自己的目的,我一直在尝试进行测试运行

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>";
?>

3 个答案:

答案 0 :(得分:1)

您的SQL语句应如下所示:

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

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

答案 1 :(得分:1)

你写的是

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

这里的表当然不是coursename。 试试这个

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

答案 2 :(得分: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>";
?> `