产品导出到xml,使用sql查询

时间:2018-05-21 10:05:21

标签: php sql xml export prestashop

我有一个sql查询,用完了输出我会完美。

现在我想在php脚本中使用sql查询,每次触发php时都会给出一个xml输出。

这是我现在的代码,但它出现了500错误(我相信错误是在第24 - 25行中创建的)

<?php 

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT
    psc5_product.id_product AS 'product_id',
    psc5_product.ean13 AS 'ean13',
    psc5_product.price AS 'price',
    psc5_product.reference AS 'product_reference',
    psc5_stock_available.quantity AS 'available_stock',
    psc5_manufacturer.name AS 'brand',
    psc5_product_lang.description AS 'description',
    psc5_product_lang.name AS 'title',
    concat("https://www.natureldeluxe.be/", psc5_category_lang.link_rewrite, "/", psc5_product_lang.link_rewrite) AS 'deeplink',
    concat("https://www.natureldeluxe.be/", psc5_image.id_image, "-large_default/", psc5_product_lang.link_rewrite, ".jpg") AS 'imagelink' 
FROM
    psc5_product 
    INNER JOIN
        psc5_stock_available 
        ON psc5_stock_available.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_manufacturer 
        ON psc5_manufacturer.id_manufacturer = psc5_product.id_manufacturer 
    INNER JOIN
        psc5_product_lang 
        ON psc5_product_lang.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_category_lang 
        ON psc5_category_lang.id_category = psc5_product.id_category_default 
    INNER JOIN
        psc5_image 
        ON psc5_image.id_product = psc5_product.id_product"

$result = $adb->query($sql);
 $xml .= "<products>";
while($row=$adb->fetch_array($result))
{
    $xml .= "<product_id>".$row['fname']."</product_id>"; 
    $xml .= "<ean13>".$row['lname']."</ean13>";
    $xml .= "<price>".$row['lname']."</price>";
    $xml .= "<product_reference>".$row['lname']."</product_reference>";
    $xml .= "<available_stock>".$row['lname']."</available_stock>";
    $xml .= "<brand>".$row['lname']."</brand>";
        $xml .= "<description>".$row['lname']."</description>";
            $xml .= "<title>".$row['lname']."</title>";
            xml .= "<deeplink>".$row['lname']."</deeplink>";
            xml .= "<imagelink>".$row['lname']."</imagelink>";
            }
$xml .= "</products>";

$sxe = new SimpleXMLElement($xml);
$sxe->asXML("test.xml");
?>

2 个答案:

答案 0 :(得分:0)

问题是您的$sql字符串中有未转义的双引号。此外,您在作业结束时错过了;。试试这个:

$sql = "SELECT
    psc5_product.id_product AS 'product_id',
    psc5_product.ean13 AS 'ean13',
    psc5_product.price AS 'price',
    psc5_product.reference AS 'product_reference',
    psc5_stock_available.quantity AS 'available_stock',
    psc5_manufacturer.name AS 'brand',
    psc5_product_lang.description AS 'description',
    psc5_product_lang.name AS 'title',
    concat(\"https://www.natureldeluxe.be/\", psc5_category_lang.link_rewrite, \"/\", psc5_product_lang.link_rewrite) AS 'deeplink',
    concat(\"https://www.natureldeluxe.be/\", psc5_image.id_image, \"-large_default/\", psc5_product_lang.link_rewrite, \".jpg\") AS 'imagelink' 
FROM
    psc5_product 
    INNER JOIN
        psc5_stock_available 
        ON psc5_stock_available.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_manufacturer 
        ON psc5_manufacturer.id_manufacturer = psc5_product.id_manufacturer 
    INNER JOIN
        psc5_product_lang 
        ON psc5_product_lang.id_product = psc5_product.id_product 
    INNER JOIN
        psc5_category_lang 
        ON psc5_category_lang.id_category = psc5_product.id_category_default 
    INNER JOIN
        psc5_image 
        ON psc5_image.id_product = psc5_product.id_product";

您还需要重写处理代码。您在代码开头有$conn作为连接的名称,但正在进一步使用$adb。此外,输出xml的行在使用查询中适当命名的变量时都使用相同的.$row['lname']变量。尝试将该代码更改为:

$result = $conn->query($sql);
$xml .= "<products>";
while ($row = $result->fetch_array())
{
    $xml .= "<product_id>".$row['product_id']."</product_id>"; 
    $xml .= "<ean13>".$row['ean13']."</ean13>";
    $xml .= "<price>".$row['price']."</price>";
    $xml .= "<product_reference>".$row['product_reference']."</product_reference>";
    $xml .= "<available_stock>".$row['available_stock']."</available_stock>";
    $xml .= "<brand>".$row['brand']."</brand>";
    $xml .= "<description>".$row['description']."</description>";
    $xml .= "<title>".$row['title']."</title>";
    $xml .= "<deeplink>".$row['deeplink']."</deeplink>";
    $xml .= "<imagelink>".$row['imagelink']."</imagelink>";
}
$xml .= "</products>";

答案 1 :(得分:0)

尝试oop xml SimpleXMLElement。 The SimpleXMLElement class

使用 SimpleXMLElement - &gt; addAttribute() SimpleXMLElement - &gt;的addChild()

就像:

<?PHP
$sxe = simplexml_load_string(/*....xml string....*/);
$sxe -> addChild("product_id", $row['fname']);