PHP 从 Mysql 数据创建 XML 文件?

标签 php mysql xml flash

我有一个 PHP 文件,它将显示 Mysql 数据库中的一些产品。这没有任何问题。

但我需要从这个 PHP 文件创建一个 XML 文件,以便能够将它加载到闪存中。

我已经完成了大部分工作,PHP 文件在服务器上创建了一个 XML 文件并提取数据(仅文本格式数据,即产品名称、价格、详细信息、添加日期等)并且它工作得很好但是,我不知道图像部分该怎么做!!

这是原始的 PHP 文件:

<?php 
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "storescripts/connect_to_mysql.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
        <tr>
          <td width="17%" valign="top"><a href="product.php?id=' . $id . '"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="77" height="102" border="1" /></a></td>
          <td width="83%" valign="top">' . $product_name . '<br />
            $' . $price . '<br />
            <a href="product.php?id=' . $id . '">View Product Details</a></td>
        </tr>
      </table>';
    }
} else {
    $dynamicList = "We have no products listed in our store yet";
}
mysql_close();
?>

这是创建 XML 文件的 PHP 文件:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php 
header("Content-Type: text/xml"); //set the content type to xml
// Initialize the xmlOutput variable
$xmlBody = '<?xml version="1.0" encoding="ISO-8859-1"?>';
$xmlBody .= "<XML>";
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "../config/connect_to_mysql.php"; 
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 6");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $price = $row["price"];
             $image = $row["<a href="../product.php?id=' . $id . '"><img src="../inventory_images/' . $id . '.jpg" alt="' . $product_name . '"/></a>"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
    $xmlBody .= '
<Data> 
    <DataID>' . $id . '</DataID> 
    <DataTitle>' . $product_name . '</DataTitle>
    <DataDate>' . $price . '</DataDate>
    <DataImage>' . $image . '</DataImage>
    <DataDescr>' . $date_added . '</DataDescr>
</Data>';
} // End while loop
mysql_close(); // close the mysql database connection
$xmlBody .= "</XML>";
echo $xmlBody; // output the gallery data as XML file for flash
}
?>
<?php echo $dynamicList; ?>

如您所见,我放置了这行代码:

$image = $row["<a href="../product.php?id=' . $id . '"><img src="../inventory_images/' . $id . '.jpg" alt="' . $product_name . '"/></a>"];

在上面的代码中,但我不断收到此错误: 解析错误:语法错误,意外的“。”在第21行,第21行就是我上面提到的那行代码!

非常感谢您的帮助。

谢谢

最佳答案

您没有正确使用双引号和单引号,您可能需要删除 $row[] 部分。 你的线路

$image = $row["<a href="../product.php?id=' . $id . '"><img src="../inventory_images/' . $id . '.jpg" alt="' . $product_name . '"/></a>"];

应该是

$image = "<a href='../product.php?id=" . $id . "'><img src='../inventory_images/" . $id . ".jpg' alt='" . $product_name . "'/></a>";

关于PHP 从 Mysql 数据创建 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15498261/

相关文章:

php - 在没有 dl() 函数的情况下动态加载 php 扩展?

mysql - SQL 查询使用 SQL 查询从 WooCommerce 选择所有产品

python - 字段未显示在 product.template odoo8 中

php - 当 UserID = 1 且 CourseID 不在数组中时删除?

php - 如何使用具有 UTF 16 编码的 PHP 函数 fopen() 创建文件?

mysql - 优化mysql ORDER BY col_a * col_b的性能,不添加col_c=col_a*col_b

mysql - 查询创建 - 我是否使用 SUM、Group 等?

android - CSS 样式的自定义字体字样

xml - 使用 XSLT 检索 XML 文件名

php - 仅当 Nginx 文件不存在时才将 URL 重定向到 PHP