Join two MySQL tables with PHP

0 votes

I have created two pages and two MySQL tables.

Index.php & citys.php

citys

 ID     City       Country  Population
 --------------------------------------
 1      Amsterdam     NL     1500000
 2      Rotterdam     NL     900000
 3      Dusseldorf    DE     1800000

comments

ID   City        Name   Comment
---------------------------------
 1   Dusseldorf  Jack   Great city!
 2   Dusseldorf  John   Beautiful
 3   Rotterdam   Emy    Love it

At the moment I only use the table citys like this:

index.php linking to citys.php with:

<a href='citys.php?cmd=menu&id=";echo $row['id'];echo "'>

And citys.php use this code to show the data from MySQL:

<?php
    include "connect.php";
    if(!isset($cmd))
    {
        if($_GET["cmd"]=="menu" || $_POST["cmd"]=="menu")
        {
            if (!isset($_POST["submit"]))
            {
                $id = $_GET["id"];
                $sql = "SELECT * FROM citys WHERE id=$id";
                $result = mysql_query($sql);
                $row = mysql_fetch_array($result);
?>

<?php echo $row["City"] ?>
<br><br>

<?php echo $row["Country"] ?>
<br><br>
<?php echo $row["Population"] ?>

I want to show the comments on page 2. So the query has to be edited to also get the right data from the table comments.

Jul 30, 2022 in PHP by Kithuzzz
• 38,010 points
880 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In PHP

0 votes
1 answer

Connection with MySQL server using PHP. How can we do that?

Hey @kartik, You have to provide MySQL hostname, ...READ MORE

answered Mar 27, 2020 in PHP by Niroj
• 82,880 points
1,008 views
0 votes
1 answer

How to convert from MySQL datetime to another format with PHP?

Hello, To convert a date retrieved from MySQL ...READ MORE

answered May 19, 2020 in PHP by Niroj
• 82,880 points
2,988 views
0 votes
1 answer

Error:MySQL Update Inner Join tables query

Hello @kartik, Try this: UPDATE business AS b INNER JOIN ...READ MORE

answered Aug 19, 2020 in PHP by Niroj
• 82,880 points
1,879 views
0 votes
0 answers

Getting PHP registration form to connect with MYSQL

I'm building a test site and my ...READ MORE

May 30, 2022 in PHP by Kichu
• 19,050 points
346 views
0 votes
0 answers

PHP with MySQL 8.0+ error: The server requested authentication method unknown to the client

I'm running MySQL version 8 on PHP ...READ MORE

Jun 18, 2022 in PHP by narikkadan
• 63,420 points
938 views
0 votes
0 answers

mysql not working with php

 I'm using Apache2, php5, and MySQL. Every time I try ...READ MORE

Jun 24, 2022 in PHP by narikkadan
• 63,420 points
261 views
0 votes
2 answers

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

HI.. SQL is Structured Query Language, which is ...READ MORE

answered Aug 8, 2020 in PHP by anonymous
9,651 views
0 votes
1 answer

Why is not preferable to use mysql_* functions in PHP?

The reasons are as follows: The MySQL extension: Does ...READ MORE

answered Sep 7, 2018 in Database by DataKing99
• 8,240 points
949 views
0 votes
2 answers
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP