如何在PHP页面中诊断MySQL查询失败


How do I diagnose a MySQL query failure in a PHP page?

我正在尝试创建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/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>

然而,我得到以下错误。我尝试了很多改变,但总是会出现不同的错误。有人在上面的代码中看到导致下面错误的问题吗?

<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_更好的东西!继续使用mysql和intertoobz wil-pwn-ur w3bs1te,srsly。话虽如此,我将在你提出的背景下回答你的问题。

当你从中解开HTML时,你的第一条错误消息是这样的:

警告:mysql_fetch_assoc():提供的参数不是有效的mysql中的结果资源/第8行上的hermes/bosraweb060/b2763/ipg.example/sitemap-brands.php

您可以查看代码的第8行,发现只有一个参数传递给该函数。这个论点是由代码中的这一行产生的:

 $brands = mysql_query("SELECT * FROM brands WHERE brand_status = 1"); 

所以,您试图从mysql_query中获取的$brands是不好的(它不是一个有效的mysql结果资源)。

可能出了什么问题?SELECT语句失败。

这可能是因为您的数据库不包含brands表,或者因为它确实包含brands表,但该表没有brand_status列。

这也可能是因为在运行查询之前需要调用mysql_select_db("数据库")。看看这个:http://www.php.net/manual/en/function.mysql-select-db.php

您可能应该在调用mysql_query()之后立即调整代码以包含以下行。这将导致您的程序向您显示MySQL本身的错误消息,而不仅仅是PHP,这样您就可以在SELECT中找出问题所在。

if (0 != mysql_errno()) { 
  echo mysql_errno() . ": " . mysql_error(). "<br/>'n";
  exit ("MySQLfailure.<br/>'n");
}

您可以将其放在每个查询之后,并将其保留在生产代码中。