由于我如何在表格中获取信息,我需要建立一个联合来显示交叉引用。
Tablebrand1 是表的名称。2 和 3
Tablebrand1
CODE CROSSREFERENCE
A15 SAMSUNG A10
A16 SAMSUNG A20
A63 SAMSUNG A30
Tablebrand2
CODE CROSSREFERENCE
X63 SAMSUNG A10
X64 SAMSUNG A20
X65 SAMSUNG A30
Tablebrand3
CODE CROSSREFERENCE
TOP99 SAMSUNG A10
TOP98 SAMSUNG A20
TOP97 SAMSUNG A30
SQL查询
(SELECT "Tablebrand1" AS `brand`, CROSSREFERENCE as `code`
FROM Tablebrand1 WHERE CROSSREFERENCE = 'SAMSUNG A10')
UNION
(SELECT "Tablebrand2" AS `brand`, CROSSREFERENCE as `code`
FROM Tablebrand2 WHERE CROSSREFERENCE = 'SAMSUNG A10')
UNION
(SELECT "Tablebrand3" AS `brand`, CROSSREFERENCE as `code`
FROM Tablebrand3 WHERE CROSSREFERENCE = 'SAMSUNG A10');
查询正确地显示了它,但是当尝试在 php 中显示它时,我收到一个来自查询的错误,因为如果我修改它以进行更简单的查询,它会正确地显示数据。
<?php
echo "<table style='border: solid 1px black;'>";
echo "<tr><th>brand</th><th>code</th></tr>";
class TableRows extends RecursiveIteratorIterator {
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}
function current() {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}
function beginChildren() {
echo "<tr>";
}
function endChildren() {
echo "</tr>" . "\n";
}
}
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("((SELECT "Tablebrand1" AS `brand`, CROSSREFERENCE as `code`
FROM Tablebrand1 WHERE CROSSREFERENCE = 'SAMSUNG A10')
UNION
(SELECT "Tablebrand2" AS `brand`, CROSSREFERENCE as `code`
FROM Tablebrand2 WHERE CROSSREFERENCE = 'SAMSUNG A10')
UNION
我的代码如下,我对 php 和 union 的了解很少。
白板的微信