从表连接获取信息的问题在PHP中显示

时间:2017-01-14 06:49:29

标签: php mysql

{连接2张桌子(带教师ID和教师姓名)

我已经创建了连接,并且已经在SQL中进行了测试。似乎很好。我想在屏幕上打印它。

$classandteacher = "SELECT person_name FROM people RIGHT OUTER JOIN classes ON classes.instructor_id=people.instructor_id ASC";

$result = mysqli_query($dbc, $classandteacher){

            while($row = mysqli_query($dbc, $result));

            $teacher = $row["person_name"];
            echo ("Teacher: " . $teacher . "<br>");}

2 个答案:

答案 0 :(得分:0)

这是因为While循环格式不正确。

删除;并将其放入{}

您还需要使用mysqli_fetch_assoc($result)从查询中获取Array

$classandteacher = "SELECT person_name FROM people RIGHT OUTER JOIN classes ON classes.instructor_id=people.instructor_id ASC";

    $result = mysqli_query($dbc, $classandteacher);

    if(!$result) { //If your MYSQL query is throwing an error
       error_log(mysqli_error());
    }


    while($row = mysqli_fetch_assoc($result))
    {
      $teacher = $row["person_name"];
      echo "Teacher: " . $teacher . "<br>";
     }

答案 1 :(得分:0)

或尝试使用     而不是mysqli_fetch_array

$classandteacher = 
"SELECT 
person_name FROM 
people RIGHT OUTER     JOIN classes ON 
classes.instructor_id
=
peopleinstructor_id     ASC";
$result =  
mysqli_query($dbc,$     classandteacher);
if(!$result) { //If 
your MYSQL query is 
throwing an error
error_log(mysqli_er
ror() );}
while($row = 
mysqli_fetch_array(
$result))
{
 $teacher = $row
["person_name"];
  echo "Teacher: " . $
teacher . "<br>"; }