簡體   English   中英

回聲內部聯接到$ row

[英]Echo inner join to $row

您好我有這個查詢來填充數據庫中的表。

$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']上回應呢?

您可以在此處使用Alias作為列名,例如:

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可用於為列或表創建臨時名稱。

有用的鏈接

使用方法:

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

而是SELECT * FROM clientes

並且您在while擁有$row['grupo']的值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM