2

我正在尝试使用 MySQL 和 PHP (v7.*) 从 WooCommerce 中以 XML 格式导出产品。我使用代码创建了自定义表并在那里添加了产品:

INSERT INTO `xml_export`(`id`, `product_name`, `product_link`) SELECT p.id, p.post_title, p.guid FROM wp_posts p WHERE p.post_type='product';

UPDATE xml_export, wp_postmeta 
SET xml_export.product_price=wp_postmeta.meta_value 
WHERE wp_postmeta.meta_key='_price' 
AND wp_postmeta.post_id=xml_export.id;

UPDATE xml_export, wp_postmeta 
SET xml_export.product_image=wp_postmeta.meta_key 
WHERE wp_postmeta.meta_key='_thumbnail_id' 
AND wp_postmeta.post_id=xml_export.id;

之后,我使用Google guide创建了 php 文件。

我的 PHP 代码是(我已经从这个片段中排除了连接数据):

<?php /*here comes connection data*/ ?>
<?php
require("phpsqlajax_dbinfo.php");

function parseToXML($htmlStr)
{
$xmlStr=str_replace('<','&lt;',$htmlStr);
$xmlStr=str_replace('>','&gt;',$xmlStr);
$xmlStr=str_replace('"','&quot;',$xmlStr);
$xmlStr=str_replace("'",'&#39;',$xmlStr);
$xmlStr=str_replace("&",'&amp;',$xmlStr);
return $xmlStr;
}

// Opens a connection to a MySQL server
$connection=mysql_connect ('localhost', $username, $password);
if (!$connection) {
  die('Not connected : ' . mysql_error());
}

// Set the active MySQL database
$db_selected = mysql_select_db($database, $connection);
if (!$db_selected) {
  die ('Can\'t use db : ' . mysql_error());
}

// Select all the rows in the table
$query = "SELECT * FROM xml_export WHERE 1";
$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

header("Content-type: text/xml");

// Start XML file, echo parent node
echo '<items>';

// Iterate through the rows, printing XML nodes for each
while ($row = @mysql_fetch_assoc($result)){
  // Add to XML document node
  echo '<item ';
  echo 'name="' . parseToXML($row['product_name']) . '" ';
  echo 'link="' . parseToXML($row['product_link']) . '" ';
  echo 'price="' . $row['product_price'] . '" ';
  echo 'image="' . $row['product_image'] . '" ';
  echo '/>';
}

// End XML file
echo '</items>';
?>

但是当我启动代码时,什么也没有发生。没有带有产品列表的有趣 XML!任何想法如何获得我需要的产品?

4

0 回答 0