我有一個PHP文件,將顯示來自Mysql數據庫的一些產品。這工作沒有任何問題。PHP從Mysql數據創建XML文件?
但我需要從這個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>"];
在上面的代碼中,但我不斷收到此錯誤: 解析錯誤:sy ntax錯誤,意外的'。'在第21行和第21行是我上面提到的代碼行!
我將不勝感激您的幫助。
感謝
生成XML文件使用PHP和MySQL - http://www.kvcodes.com/2017/03/generate-xml-file-using-php- mysql/ – Kvvaradha 2017-04-02 01:02:48