<?php
mysql_connect("localhost", "****", "****") or die(mysql_error());
mysql_select_db("****") or die(mysql_error());
// Make a MySQL Connection
$query = "SELECT * FROM sk";
$result = mysql_query($query) or die(mysql_error());
$myFile = 'sitemap-sk.txt';
$myContent ="";
while($row = mysql_fetch_array($result)){
$myContent .= "<url><loc>/".$row['id']."-".$row['company_name'].".html</loc>
<changefreq>monthly</changefreq></url>\n\r";
}
file_put_contents($myFile, $myContent);
?>尝试将此数据写入sitemap-sk.txt,但没有任何成功
发布于 2011-07-05 14:16:49
试试这个,我认为它是有效的:
$myFile = 'sitemap-sk.txt';
$fp=fopen($myFile,'a+')
while($row = mysql_fetch_assoc($result)){
fwrite( $fp, "<url><loc>/".$row['id']."-".$row['company_name'].".html</loc><changefreq>monthly</changefreq></url>\n\r");
}
fclose($fp);编辑: 试试这个,然后对我说结果:
while($row = mysql_fetch_assoc($result)){
$content[] = "<url><loc>/".$row['id']."-".$row['company_name'].".html</loc><changefreq>monthly</changefreq></url>\n\r";
}
var_dump($content);发布于 2011-07-05 14:24:17
在不使用MySQL部件的情况下尝试您的代码在mi情况下可以完美地工作。这就是:
$myFile = 'sitemap-sk.txt';
$myContent ="some test content";
file_put_contents($myFile, $myContent);由于您提到的行为,我认为您可能存在MySQL问题。让我来解释一下:您已经将...or die(mysql_error());放在了每一行有MySQL语句的行中。这会导致脚本在遇到错误时突然终止,而不输出。因此,该脚本实际上从未从您的数据库中提取数据。
您应该调试是哪一行导致触发die()。我建议您删除...or die(mysql_error());调用,并使用类似于(从我的头顶)的东西:
if ($result = mysql_query($query)) {
do stuff
}
else {
echo "query error\n";
echo mysql_error();
}干杯,
https://stackoverflow.com/questions/6578523
复制相似问题