使用JOIN在表中显示数据

时间:2013-08-25 14:59:55

标签: php join mysqli

感谢您阅读我的问题

我正在尝试将 repair_jobs 中的* clients_id *显示为联系人

中的名称

但我没有运气 我有2个SQL查询是错的吗?

第1次

$query = "select * from repair_jobs";

这有助于我显示我需要的有关repair_jobs和作品

字段的信息

这是第二次

$query = "SELECT repair_jobs.client_id, contacts.name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.name";

在此之下,我试图显示客户端的名称

echo "<td>{$client_id}</td>";

但它只显示我需要的数字而不是数据(客户名称)

我错过了什么吗?


其他信息

client_id(repair_jobs)是一个数字,与id(联系人)相同,但想要显示名称(联系人)

客户

Id – name – surname – phone – address

修理

Id – clients_id (same as id in clients) – unit – date – price

当前代码

<?php
//include database connection
include 'db_connect.php';

//query all records from the database
$query = "select * from repair_jobs";

//execute the query
$result = $mysqli->query( $query );

//get number of rows returned
$num_results = $result->num_rows;

//this will link us to our add.php to create new record
if( $num_results > 0){ //it means there's already a database record

    //start table
    //creating our table heading
    echo " <table class='table_basic'>";
    echo "<thead><tr>";
        echo "<th>Job #</th>";
        echo "<th>Name Of Unit</th>";
        echo "<th>Client</th>";
        echo "<th>Estimated Value</th>";
    echo "</thead></tr><tbody><tr>";

    //loop to show each records
    while( $row = $result->fetch_assoc() ){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td width='40px'><a href='rdetails.php?id={$id}'># {$id}</a></td>";
                echo "<td>{$rmake} {$rmodel}</td>";

$query = "SELECT rj.client_id, c.name AS client_name FROM repair_jobs rj INNER JOIN contacts c ON rj.client_id=c.id";
echo "<td>{$client_name}</td>";

echo '<td align="center"><span class="badge badge-success">£';
$lhours = $labour;
$repaircosts = $ourcosts;
$labourpay = $labourcharge;
$sum_total = $repaircosts +($lhours * $labourpay);




print ($sum_total);
echo '</span></td>';
echo "</td>";
echo "";
    }

    echo "</tr></table>";//end table

}else{
    //if database table is empty
    echo "No records found.";
}

//disconnect from database
$result->free();
$mysqli->close();

?>

2 个答案:

答案 0 :(得分:2)

将第一个查询更改为连接查询,因为没有理由在代码中间执行第二个查询。 (你也从未执行过那个查询)。

//query all records from the database
$query = "SELECT repair_jobs.*, contacts.name as client_name
FROM repair_jobs
INNER JOIN contacts
ON repair_jobs.client_id=contacts.id";

然后在你的表格中保留$client_name

echo "<td>{$client_name}</td>";

答案 1 :(得分:1)

<?php
include 'db_connect.php';
$query = "SELECT rj.Id AS job_number, rj.unit, rj.make, rj.model, c.name AS client_name, rj.price FROM repair_jobs rj INNER JOIN contacts c ON rj.clients_id = c.id ORDER BY c.date";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
if( $num_results > 0){ //it means there's already a database record
    echo " <table class='table_basic'>";
    echo "<thead><tr>";
    echo "<th>Job #</th>";
    echo "<th>Name Of Unit</th>";
    echo "<th>Client</th>";
    echo "<th>Estimated Value</th>";
    echo "</tr></thead><tbody>";
    while( $row = $result->fetch_assoc() ){
        extract($row);
        echo "<tr>";
        echo "<td width='40px'><a href='rdetails.php?id={$job_number}'>#{$job_number}</a></td>";
        echo "<td>{$make} {$model}</td>";
        echo "<td>{$client_name}</td>";
        echo "<td align='center'><span class='badge badge-success'>£";
        $lhours = $labour;
        $repaircosts = $ourcosts;
        $labourpay = $labourcharge;
        $sum_total = $repaircosts +($lhours * $labourpay);
        echo $sum_total;
        echo '</span></td>';
        echo "</td>";
        echo "</tr>";
    }
    echo "</tbody></table>";
} else {
    echo "No records found.";
}
$result->free();
$mysqli->close();
?>