来自不同表的mysql的PHP图像显示

标签 php mysql

<分区>

我正在为一个类(class)创建一个二手车网站。我有一个有两个表的 mysql 数据库。一张用于汽车细节,一张用于汽车照片。我想在一些细节旁边显示一张汽车的单张照片(就像你在 cars.com 上看到的那样),但我很挣扎。汽车 ID 是一张表,“ext_1”在另一张表上。

<?php

$page_title = "List cars";

require_once ('includes/header.php');
require_once('includes/database.php');

//select statement
$sql = "SELECT * FROM inventory_tbl, inventory_photos_tbl";
//execute the query
$query = $conn->query($sql);

//Handle selection errors
if (!$query) {
$errno = $conn->errno;
$errmsg = $conn->error;
echo "Selection failed with: ($errno) $errmsg<br/>\n";
$conn->close();
//require_once ('includes/footer.php');
exit;
}
//display results in a table
?>
<h2>Inventory</h2>

<table border="1px solid" border="collapse" align ="center" height='300'  width='750' >
<tr><center>
            <th></th>
    <th>Year</th>
    <th>Make</th>
    <th>Model</th>
    <th>Mileage</th>
            <th>Price</td>
    <th>Details</th>
</center>
</tr>

<?php
//insert a row into the table for each row of data
    while (($row = $query->fetch_assoc()) !==NULL){
        echo "<tr>";
        echo "<td><img src =" .$row['id']. $row['ext_1']. " width = 100     height = 100></td>";
        echo "<td>", $row['year'], "</td>";
        echo "<td>", $row['make'], "</td>";
        echo "<td>", $row['model'], "</td>";
        echo "<td>", number_format($row['mileage']), "</td>";
        echo "<td>","$", number_format($row['price']), "</td>";
        echo "<td><a href='cardetails.php?id=", $row['id'],"'>View Details</td>";
        echo "</tr>";
    }
?>

<?php
// clean up resultsets when we're done with them!
$query->close();

// close the connection.
$conn->close();

//include the footer
require_once ('includes/footer.php');

最佳答案

我将我的答案从评论中移出,以便 future 的读者能够从该解决方案中受益。

首先,查询需要限定连接条件,因此将查询更改为:

$sql = "SELECT * FROM inventory_tbl, inventory_photos_tbl WHERE inventory_tbl.id = inventory_photos_tbl.ext_1"

其次,img 标签应该只引用照片来源/列,所以它应该是这样的:

echo "<td><img src =" .$row['myPhotoColumn']. " width = 100     height = 100></td>";

关于来自不同表的mysql的PHP图像显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33614823/

相关文章:

php - 获取 GMail、Yahoo、Hotmail 等联系人

php - mysql查询结果排序

javascript - 如何将变量传递给 bootstrap 模式表单并运行 mysql 查询

php - Laravel 重定向到路由,但随后 apache 给出 404 错误

php - 从sql迁移到mysqli

php - 软呢帽 PHP "failed to open stream: Permission denied"

mysql - 创建外键的正确语法

PHP单选按钮mysql查询

php - 在下拉列表中从数据库返回结果并出现解析错误

MySQL 基于日期列按周分组?