📜  PHP – Mysql 加入

📅  最后修改于: 2022-05-13 01:54:56.004000             🧑  作者: Mango

PHP – Mysql 加入

在本文中,我们将使用PHP连接两个表并将它们显示在网页上。

介绍 :

PHP是一种服务器端脚本语言,用于连接数据库。使用它,我们可以使用PHP脚本从数据库中获取数据。可用于与PHP通信的数据库语言是 MySQL。 MySQL 是一种数据库查询语言,用于管理数据库。

要求 :

Xampp 服务器 – xampp 服务器用于在本地存储我们的数据库。我们将使用PHP从 xampp 服务器访问数据。



在本文中,我们使用包含两个表的学生详细信息数据库。它们是 student_address 和 student_marks。

表的结构:

table1=student_address

table2=student_marks。

我们将在这两个表上执行 INNER JOIN、LEFT JOIN、RIGHT JOIN。

1. 内连接:



INNER JOIN 是一个关键字,用于选择在两个表中具有匹配值的记录。

句法 :

SELECT column 1,column 2,...column n
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;

例子 :

让 student_address 包含这些详细信息

而 student_marks 表包括

通过使用 sid,我们可以使用内连接来连接这两个表,因为 sid 在两个表中是通用的。

  • 查询以根据内连接显示学生地址详细信息 –
SELECT  * from student_address INNER JOIN student_marks on student_address.sid=student_marks.sid;

结果 :

STUDENT-ID : 1 ----- NAME : sravan kumar ----- ADDRESS : kakumanu
STUDENT-ID : 2 ----- NAME : bobby ----- ADDRESS : kakumanu
STUDENT-ID : 3 ----- NAME : ojaswi ----- ADDRESS : hyderabad
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad
  • 查询以显示基于内部联接的 student_marks 详细信息。
SELECT  * from student_marks INNER JOIN student_address on student_address.sid=student_marks.sid

结果 :



STUDENT-ID : 1 ----- SUBJECT 1 : 98 ----- SUBJECT 2 : 99
STUDENT-ID : 2 ----- SUBJECT 1 : 78 ----- SUBJECT 2 : 89
STUDENT-ID : 3 ----- SUBJECT 1 : 78 ----- SUBJECT 2 : 98
STUDENT-ID : 4 ----- SUBJECT 1 : 89 ----- SUBJECT 2 : 98

2.左加入:

LEFT JOIN 关键字用于返回左表(table1)中的所有记录,以及右表(table2)中匹配的记录。

句法 :

SELECT column1,column2,...columnn
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
  • 使用左连接查询以根据学生 ID 显示所有学生地址表
SELECT * from student_address LEFT JOIN student_marks on student_address.sid=student_marks.sid

结果 :

STUDENT-ID : 1 ----- NAME : sravan kumar ----- ADDRESS : kakumanu
STUDENT-ID : 2 ----- NAME : bobby ----- ADDRESS : kakumanu
STUDENT-ID : 3 ----- NAME : ojaswi ----- ADDRESS : hyderabad
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad
STUDENT-ID : ----- NAME : gnanesh ----- ADDRESS : hyderabad
  • 使用左连接查询以根据学生 ID 显示所有 student_marks 表
SELECT * from student_marks LEFT JOIN student_address on student_address.sid=student_marks.sid

结果 :

STUDENT-ID : 1 ----- SUBJECT 1 : 98 ----- SUBJECT 2 : 99
STUDENT-ID : 2 ----- SUBJECT 1 : 78 ----- SUBJECT 2 : 89
STUDENT-ID : 3 ----- SUBJECT 1 : 78 ----- SUBJECT 2 : 98
STUDENT-ID : 4 ----- SUBJECT 1 : 89 ----- SUBJECT 2 : 98
STUDENT-ID : ----- SUBJECT 1 : 89 ----- SUBJECT 2 : 79

3. 正确加入:

RIGHT JOIN 关键字用于返回右表(table2)中的所有记录,以及左表(table1)中匹配的记录。

句法 :

SELECT column1,column2,...columnn
FROM table1
RIGHT  JOIN table2
ON table1.column_name = table2.column_name;
  • 使用右连接查询以根据学生 ID 显示所有学生地址表
SELECT * from student_address RIGHT JOIN student_marks on student_address.sid=student_marks.sid

结果 :

STUDENT-ID : 1 ----- NAME : sravan kumar ----- ADDRESS : kakumanu
STUDENT-ID : 2 ----- NAME : bobby ----- ADDRESS : kakumanu
STUDENT-ID : 3 ----- NAME : ojaswi ----- ADDRESS : hyderabad
STUDENT-ID : 4 ----- NAME : rohith ----- ADDRESS : hyderabad
STUDENT-ID : 7 ----- NAME : ----- ADDRESS :
  • 查询以使用右联接根据学生 ID 显示所有 student_marks 表
SELECT * from student_marks RIGHT JOIN student_address on student_address.sid=student_marks.sid

结果 :



STUDENT-ID : 1 ----- SUBJECT 1 : 98 ----- SUBJECT 2 : 99
STUDENT-ID : 2 ----- SUBJECT 1 : 78 ----- SUBJECT 2 : 89
STUDENT-ID : 3 ----- SUBJECT 1 : 78 ----- SUBJECT 2 : 98
STUDENT-ID : 4 ----- SUBJECT 1 : 89 ----- SUBJECT 2 : 98
STUDENT-ID : 5 ----- SUBJECT 1 : ----- SUBJECT 2 :

方法 :

  • 创建一个名为 database 的数据库并创建表(student_address 和 student_marks)
  • 使用PHP记录插入到两个表中
  • 编写 SQL 查询以使用PHP执行所有连接
  • 观察结果。

脚步:

  • 启动 xampp 服务器

  • 在浏览器中输入“localhost/phpmyadmin”并创建一个名为“database”的数据库,然后创建两个名为 student_address 和 student_marks 的表

Student_address 表结构:

Student_marks 表结构:

  • 输入“本地主机/ DATA1插入使用PHP(数据PHP)记录到student_address表上运行代码。 PHP”
PHP
connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
//insert records into table
$sql  = "INSERT INTO student_address VALUES (1,'sravan kumar','kakumanu');";
$sql .= "INSERT INTO student_address VALUES (2,'bobby','kakumanu');";
$sql .= "INSERT INTO student_address  VALUES (3,'ojaswi','hyderabad');";
$sql .= "INSERT INTO student_address  VALUES (4,'rohith','hyderabad');";
$sql .= "INSERT INTO student_address  VALUES (5,'gnanesh','hyderabad');";
  
if ($conn->multi_query($sql) === TRUE) {
  echo "data stored successfully";
} else {
  echo "Error: " . $sql . "
" . $conn->error; }    $conn->close(); ?>


PHP
connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
//insert records into table
$sql  = "INSERT INTO student_marks VALUES (1,98,99);";
$sql .= "INSERT INTO student_marks VALUES (2,78,89);";
$sql .= "INSERT INTO student_marks  VALUES (3,78,98);";
$sql .= "INSERT INTO student_marks  VALUES (4,89,98);";
$sql .= "INSERT INTO student_marks  VALUES (7,89,79);";
  
if ($conn->multi_query($sql) === TRUE) {
  echo "data stored successfully";
} else {
  echo "Error: " . $sql . "
" . $conn->error; }    $conn->close(); ?>


PHP


";
echo "
"; //sql query to display all student_address table based on student id using  inner join $sql = "SELECT  * from student_address INNER JOIN student_marks on student_address.sid=student_marks.sid"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;     echo "
";        }    echo "
"; echo "inner join on student_marks: "; echo "
"; echo "
"; //sql query to display all student_marks  table based on student id using  inner join $sql1 = "SELECT  * from student_marks INNER JOIN student_address on student_address.sid=student_marks.sid"; $result1 = $conn->query($sql1); //display data on web page while($row = mysqli_fetch_array($result1)){     echo " STUDENT-ID : ". $row['sid'], " ----- SUBJECT 1 : ". $row['subject1'] ," ----- SUBJECT 2 : ". $row['subject2'] ;     echo "
";        }       //close the connection    $conn->close(); ?>


PHP


";
echo "
"; //sql query to display all student_address table based on student id using  right join $sql = "SELECT  * from student_address RIGHT JOIN student_marks on student_address.sid=student_marks.sid"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;     echo "
";        }    echo "
"; echo "right  join on student_marks: "; echo "
"; echo "
"; //sql query to display all student_marks  table based on student id using right join $sql1 = "SELECT  * from student_marks RIGHT JOIN student_address on student_address.sid=student_marks.sid"; $result1 = $conn->query($sql1); //display data on web page while($row = mysqli_fetch_array($result1)){     echo " STUDENT-ID : ". $row['sid'], " ----- SUBJECT 1 : ". $row['subject1'] ," ----- SUBJECT 2 : ". $row['subject2'] ;     echo "
";        }       //close the connection    $conn->close(); ?>


PHP


";
echo "
"; //sql query to display all student_address table based on student id using  left join $sql = "SELECT  * from student_address LEFT JOIN student_marks on student_address.sid=student_marks.sid"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;     echo "
";        }    echo "
"; echo "left join on student_marks: "; echo "
"; echo "
"; //sql query to display all student_marks  table based on student id using left join $sql1 = "SELECT  * from student_marks LEFT JOIN student_address on student_address.sid=student_marks.sid"; $result1 = $conn->query($sql1); //display data on web page while($row = mysqli_fetch_array($result1)){     echo " STUDENT-ID : ". $row['sid'], " ----- SUBJECT 1 : ". $row['subject1'] ," ----- SUBJECT 2 : ". $row['subject2'] ;     echo "
";        }       //close the connection    $conn->close(); ?>


输出 :



编写PHP代码以在 student_marks 表中插入详细信息。 (PHP)

PHP

connect_error) {
  die("Connection failed: " . $conn->connect_error);
}
//insert records into table
$sql  = "INSERT INTO student_marks VALUES (1,98,99);";
$sql .= "INSERT INTO student_marks VALUES (2,78,89);";
$sql .= "INSERT INTO student_marks  VALUES (3,78,98);";
$sql .= "INSERT INTO student_marks  VALUES (4,89,98);";
$sql .= "INSERT INTO student_marks  VALUES (7,89,79);";
  
if ($conn->multi_query($sql) === TRUE) {
  echo "data stored successfully";
} else {
  echo "Error: " . $sql . "
" . $conn->error; }    $conn->close(); ?>

输出 :

输入“本地主机/数据2。 PHP”查看输出

编写PHP代码进行内连接(PHP)

PHP



";
echo "
"; //sql query to display all student_address table based on student id using  inner join $sql = "SELECT  * from student_address INNER JOIN student_marks on student_address.sid=student_marks.sid"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;     echo "
";        }    echo "
"; echo "inner join on student_marks: "; echo "
"; echo "
"; //sql query to display all student_marks  table based on student id using  inner join $sql1 = "SELECT  * from student_marks INNER JOIN student_address on student_address.sid=student_marks.sid"; $result1 = $conn->query($sql1); //display data on web page while($row = mysqli_fetch_array($result1)){     echo " STUDENT-ID : ". $row['sid'], " ----- SUBJECT 1 : ". $row['subject1'] ," ----- SUBJECT 2 : ". $row['subject2'] ;     echo "
";        }       //close the connection    $conn->close(); ?>

输出 :

输入“本地主机/表单。 PHP”在您的浏览器中。

写代码来执行正确的加入(Form1上PHP)

PHP



";
echo "
"; //sql query to display all student_address table based on student id using  right join $sql = "SELECT  * from student_address RIGHT JOIN student_marks on student_address.sid=student_marks.sid"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;     echo "
";        }    echo "
"; echo "right  join on student_marks: "; echo "
"; echo "
"; //sql query to display all student_marks  table based on student id using right join $sql1 = "SELECT  * from student_marks RIGHT JOIN student_address on student_address.sid=student_marks.sid"; $result1 = $conn->query($sql1); //display data on web page while($row = mysqli_fetch_array($result1)){     echo " STUDENT-ID : ". $row['sid'], " ----- SUBJECT 1 : ". $row['subject1'] ," ----- SUBJECT 2 : ". $row['subject2'] ;     echo "
";        }       //close the connection    $conn->close(); ?>

输出 :



输入“本地主机/form1。 PHP”在您的浏览器中。

编写PHP代码来执行左连接(窗口PHP)

PHP



";
echo "
"; //sql query to display all student_address table based on student id using  left join $sql = "SELECT  * from student_address LEFT JOIN student_marks on student_address.sid=student_marks.sid"; $result = $conn->query($sql); //display data on web page while($row = mysqli_fetch_array($result)){     echo " STUDENT-ID : ". $row['sid'], " ----- NAME : ". $row['sname'] ," ----- ADDRESS : ". $row['saddress'] ;     echo "
";        }    echo "
"; echo "left join on student_marks: "; echo "
"; echo "
"; //sql query to display all student_marks  table based on student id using left join $sql1 = "SELECT  * from student_marks LEFT JOIN student_address on student_address.sid=student_marks.sid"; $result1 = $conn->query($sql1); //display data on web page while($row = mysqli_fetch_array($result1)){     echo " STUDENT-ID : ". $row['sid'], " ----- SUBJECT 1 : ". $row['subject1'] ," ----- SUBJECT 2 : ". $row['subject2'] ;     echo "
";        }       //close the connection    $conn->close(); ?>

输出 :

输入本地主机/form2。浏览器中的PHP