简体   繁体   中英

PHP MYSQL Join two tables and echo Data

I have two tables

Table personal_info has the information

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

Table other_info has the information

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

Now i want to use PHP to join the tables and get information from the two tables i decided to do something like this

<?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'] ";
}


?>

How do i join the tables and get data?

you need to set a foreign key in the other_info table first, so you can join over this key.

then a select like this should work

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 is the foreign key column in the table other_info

<?php
include ('config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect : '.mysqli_error());
$sql = "SELECT personal_info.id,personal_info.fname,personal_info.lname,personal_info.email, other_info.university_name,other_info.course_name,other_info.business_name FROM personal_info FULL OUTER JOIN other_info ON personal_info.id=other_info.id;"; 
$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'] ";
}


?>

There is no relation between personal_info and other_info table. Through which, it is impossible to know the other information related to particular record.

So, you can do one thing. Make one column personal_info_id in other_info table, which will have store the id of personal_info table.

Like:

personal_info

id | fname | lname | email
1   UserFN  UserLN  user@email
2   User2FN User2LN user2@email

other_info

id | personal_info_id | university_name | course_name | business_name
1       1                 UniV               PHP           ABC
2       2                 UniVY              C++           XYZ

Now, you can join 2 tables by using personal_info_id of other_info with id of personal_info

SELECT pfo.*, ofo.* 
FROM personal_info pfo, other_info ofo 
WHERE pfo.id = ofo.personal_info_id;

Updated Code

<?php
include ('config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect : '.mysqli_error());
$sql = "SELECT pfo.*, ofo.* FROM personal_info pfo, other_info ofo WHERE pfo.id = ofo.personal_info_id;";
$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['email']." / ".$row['university_name']." / ".$row['course_name']." / ".$row['business_name'];
}?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM