从多个表中选择数据以供显示

时间:2015-04-15 17:36:11

标签: php sql

错误代码

<?php 
    include 'database.php';
    $pdo = Database::connect();   
    $sql = 'SELECT id, name, date, installer, salesman, category, status FROM workorder JOIN customers ON customers.id=workorder.name'; 

    foreach ($pdo->query($sql) as $row) {
        echo '<tr>';
        echo '<td>'. $row['id'] . '</td>';
        echo '<td>'. $row['name'] . '</td>';
        echo '<td>'. $row['date'] . '</td>';
        echo '<td>'. $row['installer'] . '</td>';
        echo '<td>'. $row['salesman'] . '</td>';
        echo '<td>'. $row['category'] . '</td>';
        echo '<td>'. $row['status'] . '</td>';
        echo '<td width=250>';
        echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
        echo '&nbsp;';
        echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
        echo '&nbsp;';
        echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
        echo '</td>';
        echo '</tr>';
    }

    Database::disconnect();
?>

结束目标:通过join&amp; amp;工作订单表中的'name'字段表示的一个表中拉出名字/姓氏。 foreach函数。

正确代码:

<?php 
                       include 'database.php';
                       $pdo = Database::connect();   


                                $sql = 'SELECT workorder.id, FName, LName, name, date, installer, salesman, category, status FROM workorder JOIN customers ON customers.id=workorder.name'; 
                                $rows = $pdo->query($sql);

                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['id'] . '</td>';
                                echo '<td>'. $row['FName'] ." ".$row['LName']; '</td>';
                                echo '<td>'. $row['date'] . '</td>';
                                echo '<td>'. $row['installer'] . '</td>';
                                echo '<td>'. $row['salesman'] . '</td>';
                                echo '<td>'. $row['category'] . '</td>';
                                echo '<td>'. $row['status'] . '</td>';
                                echo '<td width=250>';
                                echo '<a class="btn" href="read.php?id='.$row['id'].'">Read</a>';
                                echo '&nbsp;';
                                echo '<a class="btn btn-success" href="update.php?id='.$row['id'].'">Update</a>';
                                echo '&nbsp;';
                                echo '<a class="btn btn-danger" href="delete.php?id='.$row['id'].'">Delete</a>';
                                echo '</td>';
                                echo '</tr>';
                                }

                       Database::disconnect();
                      ?>

在SELECT命令中添加了“FName,LName”(因为在加入customer表后显然可以调用它)并将foreach函数分别改为包含“FName”和“LName”。添加“workorder.id”而不仅仅是“id”可能是为了防止workorder表中的id与customers表中的id冲突。

感谢每个人的帮助!!!!!!!!!!!!

1 个答案:

答案 0 :(得分:2)

您需要在选择查询中添加FName和LName列。在输出部分更改代码以检索FName和LName。

SQL:

try{
$sql = "SELECT workorder.id, FName, LName, name, date, installer, salesman, category, status FROM workorder JOIN customers ON customers.id=workorder.name"; 
$rows = $pdo->query($sql);
}catch(PDOException $pe){
var_dump($pe);
}

而不是

echo '<td>'. $row['name'] . '</td>';

你需要做

echo '<td>'. $row['FName'] ." ".$row['LName'] '</td>';