我需要从数据库表中检索一条信息,如何从db中获取mpn编号?
我在代码的第11行添加了它,但没有显示在结果中?
这是数据库的图像:
<?php
$path = str_replace('/autorun','',str_replace('/controlpanel','',dirname($_SERVER['SCRIPT_FILENAME'])).'/');
$path = '../';
require($path.'start/autorun.php');
$file = $path.'googlebase.txt';
$fh = fopen($file, 'w');
$headerline .= "id\t";
$headings[0] = 'id';
$headings[1] = 'title';
$headings[2] = 'description';
$headings[3] = 'link';
$headings[4] = 'price';
$headings[5] = 'brand';
$headings[6] = 'condition';
$headings[7] = 'image_link';
$headings[8] = 'quantity';
$headings[9] = 'model_number';
$headings[10] = 'availability';
$headings[11] = 'MPN';
fwrite($fh, implode("\t",$headings)."\n");
# Products (+ Reviews)
$products = $db->query('SELECT * FROM products WHERE parent_id = 0 AND enabled = 1 AND hidden = 0 AND id IN (SELECT product_id FROM products_categories) ORDER BY salesrank ASC LIMIT 2000');
$products = $products->getFullArray();
foreach ($products as $product)
{
$productattributes = getProductAttributes($product['id']);
$manufacturer = getManufacturer($product['manufacturer_id']);
$images = getAllProductImages($product['id'], '500', '500');
$mainimage = $images[0]; unset($images[0]);
# Write Line
$row = array();
$row[0] = $product['id'];
$row[1] = $product['title'];
$row[2] = $product['metadescription'];
$row[3] = URL_SITE.$product['pagename'].'/';
$row[4] = $product['price']/100;
$row[5] = $manufacturer['name'];
$row[6] = 'new';
$row[7] = URL_SITE.$mainimage['resized'];
$row[8] = '100';
$row[9] = $productattributes['Model'];
$row[10] = 'in stock';
$row[11] = $productattributes['mpn'];
fwrite($fh, implode("\t",$row)."\n");
}
fclose($fh);
?>
答案 0 :(得分:0)
也许你可以尝试类似的东西:
SELECT *,
(SELECT value
FROM productattributes
WHERE name = 'MPN'
AND productattributes.product_id = products.id) MPN
FROM products
WHERE parent_id = 0
AND enabled = 1
AND hidden = 0
AND id IN
(SELECT product_id
FROM products_categories)
ORDER BY salesrank ASC LIMIT 2000
这将从productattributes
中选择name
为MNP且product_id
= products
行的行。
甚至更好:
SELECT products.*,
productattributes.value
FROM products
LEFT JOIN productattributes ON (name = 'MPN' AND productattributes.product_id = products.id)
WHERE parent_id = 0
AND enabled = 1
AND hidden = 0
AND id IN
(SELECT product_id
FROM products_categories)
ORDER BY salesrank ASC LIMIT 2000