输出联接表

jgzswidk  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(233)

我当前正在尝试输出一个联接表。
两个连接的表由 Customer 表格:

以及 Address 表格:

但是我得到了这个错误:
注意:未定义索引:street
对于来自的每个联接索引,都会发生此错误 Address 表(所以它也发生在
Postcode City 以及 Country 以及)。
我的问题是如何解决这个问题?

uqdfh47h

uqdfh47h1#

为什么没有定义是因为没有使用联接行来执行。

$stmt = $pdo->prepare('SELECT * FROM Customer LEFT JOIN Address USING (AddressID)');
$stmt->execute();

上面的这段代码是获得联接代码的代码,然后立即用一个没有联接数据的新代码覆盖$stmt。
删除这两行代码。

$stmt = $pdo -> prepare('SELECT * FROM Customer');
$stmt -> execute();

它会起作用的。
顺便说一句,在customerrowoutput方法中,您使代码更难阅读。
在双引号内使用变量replace system时,可以使用数组。用大括号把它们包起来 {} 所以你现在的语法 <tr><td>$cn</td>...</tr> 应该是 <tr><td>{$customerRow['CustomerName']}</td>...</tr> 像这样 Package 数组的花括号将允许它被输出。这也节省了内存,因为您不需要创建指针来简化回音。
php还支持字符串类型变量中的换行符。这样整个功能就可以

function CustomerRowOutput($customerRow){
    return "<tr>
        <td>{$customerRow['CustomerName']}</td>
        <td>{$customerRow['PhoneNo']}</td>
        <td>{$customerRow['Email']}</td>
        <td>{$customerRow['Street']}</td>
        <td>{$customerRow['Postcode']}</td>
        <td>{$customerRow['City']}</td>
        <td>{$customerRow['Country']}</td>
    </tr>";
}

相关问题