Home > Backend Development > PHP Tutorial > How to write a PHP script in which I join two MySQL tables using MySQL JOINS?

How to write a PHP script in which I join two MySQL tables using MySQL JOINS?

王林
Release: 2023-08-20 11:58:02
forward
754 people have browsed it

如何编写PHP脚本,使用MySQL JOINS在其中连接两个MySQL表格?

We can use the MySQL JOIN syntax to connect the two tables to the PHP function mysql_query(). This function is used to execute a SQL command, and then another PHP function mysql_fetch_array() can be used to fetch all selected data.

To illustrate this, we have the following example −

Example

In this example we have used two MySQL tables with the following data−

mysql> SELECT * FROM tcount_tbl;
+-----------------+----------------+
| tutorial_author | tutorial_count |
+-----------------+----------------+
| mahran          |      20        |
| mahnaz          |      NULL      |
| Jen             |      NULL      |
| Gill            |      20        |
| John Poul       |       1        |
| Sanjay          |       1        |
+-----------------+----------------+
6 rows in set (0.01 sec)

mysql> SELECT * from tutorials_tbl;
+-------------+----------------+-----------------+-----------------+
| tutorial_id | tutorial_title | tutorial_author | submission_date |
+-------------+----------------+-----------------+-----------------+
|      1      |   Learn PHP    |   John Poul     |   2007-05-24    |
|      2      | Learn MySQL    |   Abdul S       |   2007-05-24    |
|      3      | JAVA Tutorial  |   Sanjay        |   2007-05-06    |
+-------------+----------------+-----------------+-----------------+
3 rows in set (0.00 sec)
Copy after login

Now, below is a PHP script that will join the table to select all the authors from table tutorials_tbl and will select the corresponding number of tutorials from tcount_tbl.

<?php
   $dbhost = &#39;localhost:3036&#39;;
   $dbuser = &#39;root&#39;;
   $dbpass = &#39;rootpassword&#39;;
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);

   if(! $conn ) {
      die(&#39;Could not connect: &#39; . mysql_error());
   }

   $sql = &#39;SELECT a.tutorial_id, a.tutorial_author, b.tutorial_count
   FROM tutorials_tbl a, tcount_tbl b
   WHERE a.tutorial_author = b.tutorial_author&#39;;

   mysql_select_db(&#39;TUTORIALS&#39;);
   $retval = mysql_query( $sql, $conn );

   if(! $retval ) {
      die(&#39;Could not get data: &#39; . mysql_error());
   }

   while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) {
      echo "Author:{$row[&#39;tutorial_author&#39;]} <br> ".
         "Count: {$row[&#39;tutorial_count&#39;]} <br> ".
         "Tutorial ID: {$row[&#39;tutorial_id&#39;]} <br> ".
         "--------------------------------<br>";
   }
   echo "Fetched data successfully</p><p>";
   mysql_close($conn);
?>
Copy after login

The above is the detailed content of How to write a PHP script in which I join two MySQL tables using MySQL JOINS?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template