繁体   English   中英

从SQL检索数据-PHP

[英]Retrieve data from sql - php

我需要从数据库表中检索一条信息,如何从数据库中获取mpn号?

我将其添加到代码的第11行,但未显示在结果中吗?

这是数据库的图像:

http://www.roofboxstore.org.uk/screenshot.jpg

<?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);

?>

也许您可以尝试以下方法:

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

这将从name为MNP的productattributes选择行,并且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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM