用 PHP 连接两个 MySQL 表 [英] Join two MySQL tables with PHP

查看:46
本文介绍了用 PHP 连接两个 MySQL 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我创建了两个页面和两个 MySQL 表.

For example, I have created two pages and two MySQL tables.

Index.php &citys.php

城市

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

评论

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

目前我只像这样使用citys表:

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

index.php 链接到 citys.php :

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

citys.php 使用此代码显示来自 MySQL 的数据:

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"] ?>

直到这里一切都显示出来并且工作正常.

Until here everything is showing up and working fine.

但我也想显示第 2 页上的评论.因此必须编辑查询才能从表 comments 中获取正确的数据.

But I also 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.

我尝试了来自互联网的不同示例,这些示例是我自己编辑的:

I tried different examples from internet that I have edited myself like:

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

但没有任何效果.

我该如何解决这个问题?

How can I fix this?

来自 VIPIN JAIN 的回答的查询有效,但还有一个问题:

The query from VIPIN JAIN's answer works, but there is one problem left:

查询:

$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";

如果表 'comments' 有三行,则此代码仅显示最后两行而不显示第一行:

If the table 'comments' has three rows, this code shows only the last two but not the first:

<?php
    while($row = mysql_fetch_array($result)) {
        echo "<br><br>";
        echo $row['name'];
        echo "<br>";
        echo $row['comment'];
    }
?>

如果我尝试这样做,它只会显示第一行.

And if I try this it only shows the first row.

<?php echo $row["name"] ?>
<br>
<?php echo $row["comment"] ?>

我不知道为什么第一条记录留在循环中.

I don't know why the first record is left away in the loop.

推荐答案

使用此查询

$sql = "SELECT * FROM citys LEFT JOIN comments ON comments.city=citys.city WHERE citys.id=$id";

将 leftjoin 用于此类工作

Use leftjoin for this type of work

这篇关于用 PHP 连接两个 MySQL 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆