回声内部联接到$ row

时间:2019-03-08 11:29:57

标签: php

你好,我有这个查询来从数据库中填充表。

$sql = "SELECT * FROM clientes";
                    if($result = mysqli_query($link, $sql)){
                        if(mysqli_num_rows($result) > 0){
                            echo "<table class='table table-bordered table-striped'>";
                                echo "<thead>";
                                    echo "<tr>";
                                        echo "<th>#</th>";
                                        echo "<th>Grupo</th>";
                                        echo "<th>Grupo</th>";
                                        echo "<th>Ação</th>";
                                    echo "</tr>";
                                echo "</thead>";
                                echo "<tbody>";
                                while($row = mysqli_fetch_array($result)){
                                    echo "<tr>";
                                        echo "<td>" . $row['id_cliente'] . "</td>";
                                        echo "<td>" . $row['id_grupo'] . "</td>";
                                        echo "<td>" . $row['cliente'] . "</td>";
                                        echo "<td>";
                                            echo "<a href='read.php?id=". $row['id_cliente'] ."' title='Ver Registo' data-toggle='tooltip'><span class='mdi mdi-magnify'></span></a>";
                                            echo "<a href='update.php?id=". $row['id_cliente'] ."' title='Actualizar Registo' data-toggle='tooltip'><span class='mdi mdi-pencil'></span></a>";
                                            echo "<a href='delete.php?id=". $row['id_cliente'] ."' title='Apagar Registo' data-toggle='tooltip'><span class='mdi mdi-sync'></span></a>";
                                        echo "</td>";
                                    echo "</tr>";
                                }
                                echo "</tbody>";                            
                            echo "</table>";
                            // Free result set
                            mysqli_free_result($result);
                        } else{
                            echo "<p class='lead'><em>Sem registos</em></p>";
                        }
                    } else{
                        echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
                    }

                    // Close connection
                    mysqli_close($link);
                    ?>

echo $ row ['id_grupo']中的值是数字,但与另一个表相关。

我知道要在php myadmin内部运行的查询,以便从两个表中内部联接值:

SELECT grupos.grupo 
    FROM clientes 
    INNER JOIN grupos 
    ON  clientes.id_grupo = grupos.id_grupo

我该如何在$ row ['id_grupo']上回声?

2 个答案:

答案 0 :(得分:1)

您可以在此处使用别名作为列名,例如:

SELECT clientes.id_grupo as c_group, grupos.id_grupo as g_group
// rest of your query.

然后您可以在$row中以下列方式获得这些列:

$row['c_group'] // value from clientes table
$row['g_group'] // value from groups table

旁注: MySQL ALIASES可用于为列或表创建一个临时名称。

Useful link

答案 1 :(得分:0)

使用:

SELECT clientes.*, grupos.grupo 
    FROM clientes 
    INNER JOIN grupos 
    ON  clientes.id_grupo = grupos.id_grupo

代替SELECT * FROM clientes

您在$row['grupo']中拥有值while