我有一个有效的查询(请参阅查询结果)暂时取出我没有足够的积分可用于两个以上的链接
但是,当我尝试在PHP中输出时,类别名称对于输出到表格时categoryName的categorycategory.categoryName和wantedcategory.categoryName都是相同的(参见屏幕截图):
我尝试在查询中使用别名,以不同方式输出categoryName。
我还尝试使用$row["offeredcategory.categoryName"]
和$row["wantedcategory.categoryName"]
,这会产生错误:
注意:未定义的索引:C:\ Program Files(x86)中的providedcategory.categoryName
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
$sql = "SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName, categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName
FROM customers
INNER JOIN ads ON ads.customerId = customers.customerID
INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID
LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID
LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.wantedcategoryID";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr><th></th><th colspan=2>OFFERING</th><th colspan=2>WANTING</th><th>Location</th></tr>";
//need to prevent SQL injection using ...
while($row = $result->fetch_assoc())
{
echo
'<tr>
<td><img src="images/'.$row["fileUploadLocation"]. '" width="80" height="80" class="descImage"/></td>
<td>' ."<h6>" . $row["categoryName"]. "</h6>" . "<br>"
. $row["servicesOfferedTitle"]. '</td>
<td>' . $row["servicesOfferedDescription"]. '</td>
<td>' . $row["categoryName"]. "<br>"
. $row["servicesWantedTitle"]. '</td>
<td>' . $row["servicesWantedDescription"]. ' </td>
<td>' . $row["location"]. '</td>
</tr>';
}
echo "</table>";
} else {
echo "0 results";
}
我现在尝试按照建议将别名从连接更改为Select但现在连接不起作用 Haven还没到$ row部分。
我现在尝试按照建议将别名从连接更改为选择但现在连接无法正常工作(请参见屏幕截图):
Haven还没到$ row部分。
来自manasschlcatz的 接下来尝试了第二个建议,但产生错误:注意:未定义的索引:C:\ Program Files(x86)中的providedName
$sql = "SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredName.categoryID, offeredName.categoryName, categoriesselected.wantedcategoryID, wantedName.categoryID, wantedName.categoryName
FROM customers
INNER JOIN ads ON ads.customerId = customers.customerID
INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID
LEFT OUTER JOIN categories AS offeredName ON offeredName.categoryID = categoriesselected.offeredcategoryID
LEFT OUTER JOIN categories AS wantedName ON wantedName.categoryID = categoriesselected.wantedcategoryID" ;
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>
<tr><th></th><th colspan=2>OFFERING</th><th colspan=2>WANTING</th><th>Location</th></tr>";
//need to prevent SQL injection using ...
while($row = $result->fetch_assoc())
{
echo
'<tr>
<td><img src="images/'.$row["fileUploadLocation"]. '" width="80" height="80" class="descImage"/></td>
<td>' . $row["offeredName"]. "<br>"
. $row["servicesOfferedTitle"]. '</td>
<td>' . $row["servicesOfferedDescription"]. '</td>
<td>' . $row["wantedName"]. "<br>"
. $row["servicesWantedTitle"]. '</td>
<td>' . $row["servicesWantedDescription"]. ' </td>
<td>' . $row["location"]. '</td>
</tr>';
}
答案 0 :(得分:0)
您有重复的字段名称:offeredcategory.categoryName
和wantedcategory.categoryName
都将被检索$row['categoryName']
,因此只会显示一个 - categoryName不明确但不会被MySQL拒绝,因为SQL语句很明确问题是在PHP中处理结果。简单的解决方案是:
offeredcategory.categoryName as offeredName
wantedcategory.categoryName as wantedName
并使用$row['offeredName']
或$row['wantedName']
进行检索,具体取决于您实际想要显示的内容。
完整的SQL成为:
SELECT customers.*, ads.*, categoriesselected.categoryselectedID, categoriesselected.offeredcategoryID, offeredcategory.categoryID, offeredcategory.categoryName as offeredName, categoriesselected.wantedcategoryID, wantedcategory.categoryID, wantedcategory.categoryName as wantedName
FROM customers
INNER JOIN ads ON ads.customerId = customers.customerID
INNER JOIN categoriesselected ON categoriesselected.adID = ads.adID
LEFT OUTER JOIN categories AS offeredcategory ON offeredcategory.categoryID = categoriesselected.offeredcategoryID
LEFT OUTER JOIN categories AS wantedcategory ON wantedcategory.categoryID = categoriesselected.wantedcategoryID";