2011-09-21 66 views
0

我有一個簡單的查詢和簡單的抓取行:沒有查詢結果,除非錯誤報告被打開

$quar=mysql_query("SELECT 
        COALESCE(sum(impression),0) as imps 
        ,COALESCE(sum(clk_count),0) as clks 
        ,COALESCE(sum(money_spent),0) as monspen,time 
        FROM `$table_name` 
        WHERE $uid_str $aid_str $timestr time=$temp_time_main 
        GROUP BY time 
        ORDER BY time"); 

$row=mysql_fetch_row($quar); 

echo '<pre>'; 
print_r($row); 

查詢運行正常,沒有錯誤,而不是返回false。回聲查詢並嘗試在PhpMyAdmin中,返回結果ok。

打印$row還是什麼都沒有返回,試着用fetch assoc,取數組。打開error_reporting(E_ALL);並瞧,print_r返回數組。禁用錯誤報告,不會再返回任何內容。可能是什麼問題?

+0

http://www.php.net/manual/en/function.mysql-num-rows .php – hakre

+2

print_r()不是調試工具,它不受error_reporting的影響。你確定沒有看到調試器(xdebug或類似的)的輸出而不是普通的print_r()嗎?此外,嘗試var_dump(),而mysql_query()之後的一個die(mysql_error()),以防萬一 –

+0

正如我所說的,沒有mysql_errors,它的工作與error_reporting。 $ quer的var_dump返回:類型(mysql結果)的資源(57)和$ row上的var_dump返回bool(false)。 – wintercounter

回答

0

可能這將有助於調試問題:

$query = "select COALESCE(sum(impression),0) as imps,COALESCE(sum(clk_count),0) as clks,COALESCE(sum(money_spent),0) as monspen,time from `$table_name` where $uid_str $aid_str $timestr time=$temp_time_main group by time order by time"; 

$result = mysql_query($query) || die('Error: ' . mysql_error()); 

while (($row=mysql_fetch_row($result)) !== false) { 
    var_dump($row); 
} 
+0

沒有mysql錯誤,正如我所說。看看我對原始帖子的評論。 – wintercounter

+0

好的。然後嘗試設置ini_set('display_errors',1);並在瀏覽器中啓動腳本後查看源HTML代碼。有沒有錯誤? – classic

+0

沒有mysql錯誤(說這20次現在很煩人,sry)。這有效:error_reporting(E_ALL); $ res = mysql_query($ q); $ row = mysql_fetch_row($ res); error_reporting(0);這是一種健康的方式,但它現在正在工作,直到我找到解決辦法。 – wintercounter

2

有很多的東西錯此查詢:

$quar=mysql_query("SELECT 
        COALESCE(sum(impression),0) as imps 
        ,COALESCE(sum(clk_count),0) as clks 
        ,COALESCE(sum(money_spent),0) as monspen 
        ,time 
        FROM `$table_name` 
        WHERE $uid_str $aid_str $timestr time=$temp_time_main 
        GROUP BY time 
        ORDER BY time"); 

1這是什麼where子句是什麼意思?

WHERE $uid_str $aid_str $timestr time=$temp_time_main 

where子句是通常的形式:field1 = 'A' AND field2 = 'B' AND field3 > 'C'

2插入不帶引號的$瓦爾是一個糟糕的壞主意

環繞所有$的單引號',如果他們是價值瓦爾如果它們是列名或表名,則在反引號`中。
這樣,如果$ var包含一個空格或其他可能會跳到MySQL的東西,那麼您的查詢不會被彈出。
你還需要注意,如果你沒有引用你的變量,mysql_real_escape_string()將不起作用。
但使用動態表名呈現指向靜音,請參閱下面的第3點。

3動態表或者列名是一個SQL注入等待發生
使用白名單來檢查列/表名是有效的。
看到這個問題:How to prevent SQL injection with dynamic tablenames?

4您的COALESCE看起來不對。
即使是單一null行將恢復您的總和爲0,我建議你使用此代碼來代替:

.... 
SUM(COALESCE(impression,0)) as imps 
.... 
+0

那些基於其他值生成Where語句的變量,沒有什麼可擔心的或者查詢,即使我使用了一個簡單的SELECT * – wintercounter

+1

@wintercounter,它仍然有問題,它**是一件值得擔心的事情,因爲它是一個足以運行高速公路的安全漏洞。 – Johan

+0

這裏是:其中'uid' = '10'< - 變量返回這個值,其他兩個在這種情況下是空的。現在你可以看到爲什麼我說這是無關緊要的,我在我的查詢中使用了引號... – wintercounter

相關問題