我有两张桌子

personal_info具有以下信息

+--------+--------+--------+--------+
|   id   | fname  | lname  | email  |
+--------+--------+--------+--------+
|        |        |        |        |
+--------+--------+--------+--------+


other_info具有以下信息

+--------+-----------------+--------------+----------------+
|   id   | university_name | course_name  | business_name  |
+--------+-----------------+--------------+----------------+
|        |                 |              |                |
+--------+-----------------+--------------+----------------+


现在,我想使用PHP来连接表并从两个表中获取信息,我决定这样做

<?php
include ('config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect : '.mysqli_error());
$sql = "select * from personal_info, other_info "; // Here i want to join the two tables to echo results
$result = mysqli_query($con,$sql)  or die("Error: ".mysqli_error($con));
while( $row = mysqli_fetch_array( $result, MYSQLI_ASSOC ) ){

    echo "$row['fname'] ."".$row['lname'] ."".$row['university_name'] ."".$row['course_name'] ."".$row['businessname'] ";
}


?>


如何加入表格并获取数据?

最佳答案

您需要首先在other_info表中设置一个外键,以便可以通过该键进行联接。

然后这样的选择应该工作

select fname, lname, university_name, course_name, business_name
from personal_info t1
inner join other_info t2 on (t1.id = t2.personal_id)


t2.personal_id是表other_info中的外键列

关于php - PHP MYSQL连接两个表并回显数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45276589/

10-12 16:44
查看更多