foreach循环内的内连接?

时间:2015-08-06 19:52:48

标签: php oop pdo foreach

故事: 我想为数据库中的所有产品创建缩略图,通过创建一个foreach循环,我设法获取所有数据,但是...... productfile有关系,图像准确无误且此图片有file_url,以便从表file_url中获取file我必须通过从{{抓取file_id来创建内部联接1}}表格productsfile

我的代码

file_url

问题: 在这一点上,我很好奇后果。这是在foreach循环中获取/** * @var array Collection of error messages */ $warnings = array(); // Constants from db.php in variable $dbhost = DB_HOST; $dbuser = DB_USER; $dbpass = DB_PASS; $dbname = DB_NAME; // Try to connect to PDO first try { $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT ); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING ); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch(PDOException $e) { die($warnings[] = $e->getMessage()); } // Select all products $query = "SELECT * FROM product"; $stmt = $conn->prepare($query); $stmt->execute(); $result = $stmt->fetchAll(); foreach($result as $product) { $file_id = $product['file_id']; $select_image_query = "SELECT `file`.`file_url` "; $select_image_query .= "FROM `file` "; $select_image_query .= "INNER JOIN `product` ON `file`.`file_id` = `product`.`file_id` "; $select_image_query .= "WHERE `product`.`file_id` = :file_id"; $stmt_select = $conn->prepare($select_image_query); $stmt_select->bindParam(':file_id', $file_id, PDO::PARAM_INT); $stmt_select->execute(); $stmt_select->bindColumn('file_url', $file_url); // Product thumbnail image echo "<img class=\"group list-group-image\" src=\"" . $file_url . "\" alt=\"\" />"; #code .. .. $stmt_select = null; } 的正确方法吗?如何单独获得每个产品的file_url

我非常感谢批评和其他人的观点,所以我可以学习,如果你看到了什么,请指出来!提前谢谢!

2 个答案:

答案 0 :(得分:0)

你的循环毫无意义。您获取所有产品ID的列表,然后使用该列表获取其余完全相同的记录。为什么不呢

SELECT product_id, file_id, file_url
FROM file
INNER JOIN product ON file.file_id = product.file_id

查询?

然而,使用准备好的声明和占位符的荣誉,即使它们没有必要。

请注意,作为进一步的提示,您不需要每次都不断重新准备查询。这是准备好的陈述的全部要点。你准备它们,然后用占位符的不同值一遍又一遍地执行。你的代码应该是

$stmt = prepare...
... bind ...
while(...) {
    $value_for_query = ...;
    $stmt->execute();
    $stmt->fetch();
}

答案 1 :(得分:0)

/**
* @var array Collection of error messages
*/
$warnings = array();

//  Constants from db.php in variable
$dbhost = DB_HOST;
$dbuser = DB_USER;
$dbpass = DB_PASS;
$dbname = DB_NAME;

//  Try to connect to PDO first
try {
    $conn = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass);

    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_SILENT );
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
    $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
    die($warnings[] =  $e->getMessage());
}
$strSql = "SELECT * FROM product "
        . "LEFT JOIN file ON `file`.`file_id` = `product`.`file_id` ";
$listProducts = $conn->query($strSql)->fetchAll();

foreach($listProducts as $arrProduct)
{            
    if (!empty($arrProduct['file_url'])) {
        //  Product thumbnail image
        echo "<img class=\"group list-group-image\" src=\"" . $arrProduct['file_url'] . "\" alt=\"\" />";
    }
    #code .. .. 
}