如何诊断PHP页面中的MySQL查询故障?
我正在尝试创建sitemap.xml文件,并从数据库中获取所有品牌。我有以下代码:如何诊断PHP页面中的MySQL查询故障?,php,mysql,error-handling,Php,Mysql,Error Handling,我正在尝试创建sitemap.xml文件,并从数据库中获取所有品牌。我有以下代码: <?php header("Content-type: text/xml"); echo'<?xml version=\'1.0\' encoding=\'UTF-8\'?>'; echo'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSche
<?php
header("Content-type: text/xml");
echo'<?xml version=\'1.0\' encoding=\'UTF-8\'?>';
echo'<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
include 'includes/modules/connections/db.php'; //database connection
$brands = mysql_query("SELECT * FROM brands WHERE brand_status = 1"); //Select all brands
$row_brands = mysql_fetch_assoc($brands);
do { ?>
<url>
<?php
//Check when brand pages was last time updated
$brand_update = mysql_query("SELECT * FROM user_history WHERE brand_id = ".$row_brands['brand_id']." ORDER BY uh_date DESC");
$row_brand_update = mysql_fetch_assoc($brand_update)
?>
<loc>http://www.example.com/brand/<? echo $row_brands['brand_url']; ?>/</loc>
<lastmod><?php echo $row_brand_update['uh_date']; ?></lastmod>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<?php } while ($row_brands = mysql_fetch_assoc($brands)); ?>
</urlset>
http://www.example.com/brand//
每日的
0.7
然而,我得到下面的错误。我尝试了很多改变,但总是出现不同的错误。有人看到上面代码中的问题导致下面的错误吗
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<br/>
<b>Warning</b>
: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
<b>
/hermes/bosoraweb060/b2763/ipg.example/sitemap-brands.php
</b>
on line
<b>8</b>
<br/>
<url>
<br/>
<b>Warning</b>
: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
<b>
/hermes/bosoraweb060/b2763/ipg.example/sitemap-brands.php
</b>
on line
<b>15</b>
<br/>
<loc>http://www.example.com/brand//</loc>
<lastmod/>
<changefreq>daily</changefreq>
<priority>0.7</priority>
</url>
<br/>
<b>Warning</b>
: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in
<b>
/hermes/bosoraweb060/b2763/ipg.example/sitemap-brands.php
</b>
on line
<b>22</b>
<br/>
</urlset>
警告
:mysql\u fetch\u assoc():提供的参数在资源中不是有效的mysql结果
/hermes/bosoraweb060/b2763/ipg.example/sitemap-brands.php
在线
8.
警告
:mysql\u fetch\u assoc():提供的参数在资源中不是有效的mysql结果
/hermes/bosoraweb060/b2763/ipg.example/sitemap-brands.php
在线
15
http://www.example.com/brand//
每日的
0.7
警告
:mysql\u fetch\u assoc():提供的参数在资源中不是有效的mysql结果
/hermes/bosoraweb060/b2763/ipg.example/sitemap-brands.php
在线
22
非常感谢你的帮助 切换到mysql之外的东西!继续使用mysql和intertoobz,我们将继续使用w3bs1te。话虽如此,我将根据你提出的问题来回答你的问题 当您从中解开HTML时,第一条错误消息显示: 警告:mysql\u fetch\u assoc():提供的参数不是有效的mysql 产生资源 /第8行的hermes/bosoraweb060/b2763/ipg.example/sitemap-brands.php 您可以查看代码的第8行,看到只有一个参数传递给该函数。该参数由代码中的以下行生成:
$brands = mysql_query("SELECT * FROM brands WHERE brand_status = 1");
因此,您试图从mysql_查询中获取的$brands
是不好的(它不是有效的mysql结果资源)
有什么不对劲吗?您的SELECT语句失败
这可能是因为您的数据库不包含brands
表,或者因为它确实包含brands
表,但该表没有brand\u status
列
也可能是因为在运行查询之前需要调用mysql\u select\u db(“数据库”)。看看这个:
在调用mysql\u query()之后,您可能应该调整代码以包含以下行。这将导致您的程序向您显示来自MySQL本身的错误消息,而不仅仅是PHP,这样您就可以找出选择中的错误
if (0 != mysql_errno()) {
echo mysql_errno() . ": " . mysql_error(). "<br/>\n";
exit ("MySQLfailure.<br/>\n");
}
如果(0!=mysql\u errno()){
echo mysql\u errno().“:”.mysql\u error().“
\n”;
退出(“MySQLfailure.
\n”);
}
您可以在每次查询后都将其放在正确的位置,并将其保留在生产代码中 请不要使用
mysql.*
函数,它已弃用(请参阅),并且容易受到sql注入的攻击。使用或。使用mysql_*被认为是有害的。