2012-09-01 2 views
0

sitemap.xml 파일을 만들고 내 데이터베이스에서 모든 브랜드를 가져 오려고합니다. 다음 코드를 가지고 있습니다 :PHP 페이지에서 MySQL 쿼리 오류를 어떻게 진단합니까?

<?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> 

감사합니다. 많은 도움을 받았습니다!

+2

mysql_ * 함수는 사용하지 마십시오. [* red box *] (http://php.net/manual/en/function.mysql-query.php)를 참조하십시오. SQL에 취약합니다. -주입. [* PDO *] (http://php.net/manual/en/book.pdo.php) 또는 [* MySQLi *] (http://php.net/manual/en/book.mysqli.php) . – alfasin

답변

-2

무언가로 바꾸기 mysql_! mysql과 intertoobz wil pwn ur w3bs1te, srsly를 계속 사용하십시오. 즉, 당신이 질문 한 맥락에서 당신의 질문에 대답 할 것입니다.

경고 : mysql_fetch_assoc() : 당신이 그것에서 HTML을 수습 할 때

첫 번째 오류 메시지는이 말한다 제공된 인수가 /헤르메스/bosoraweb060/b2763에 유효한 MySQL의 결과 리소스 아니다 /ipg.example/sitemap-brands.php on line 8

코드의 8 행을보고 해당 함수에 전달 된 인수가 하나만 있음을 볼 수 있습니다. 그 인수는 코드에서이 라인에 의해 제작되었다 :

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

그래서, 당신은 다시는 mysql_query에서 벗어나려고이 일을 $brands이 (유효한 MySQL의 결과 리소스 아니다) 좋은입니다.

무엇이 잘못 될 수 있습니까? SELECT 문이 실패했습니다.

데이터베이스에 brands 테이블이 없거나 테이블에 brands이 포함되어 있지만 해당 테이블에 brand_status 열이 없기 때문일 수 있습니다.

쿼리를 실행하기 전에 mysql_select_db ("database")를 호출해야 할 수도 있습니다. 이 부분을 보자. http://www.php.net/manual/en/function.mysql-select-db.php

mysql_query()를 호출 한 직후에 다음 행을 포함하도록 코드를 조정해야한다. 이렇게하면 프로그램이 PHP뿐만 아니라 MySQL 자체의 오류 메시지를 표시하므로 SELECT에서 잘못된 점을 파악할 수 있습니다.

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

모든 쿼리가 끝날 때마다이 값을 입력하고 프로덕션 코드에 그대로 둘 수 있습니다.

+0

mysql_ *를 사용하면 유해한 것으로 간주됩니다. – spacediver