2011-02-16 54 views
2

以下MySQL查詢在PHP中運行時沒有錯誤,但結果集爲空。如預期的那樣,將查詢字符串直接輸出到文件並使用'source [filename]'在MySQL客戶端中運行查詢將返回多行結果。MySQL查詢返回mysql中的行,但在PHP中設置爲空

是否有東西會導致此查詢不能與PHP一起使用? categorylinks.cl_to和smw_spec2.value_string都是varbinary(255)。顯示創建表指示引擎= InnoDB和默認字符集=二進制。

事情我都沒有成功嘗試:

  • $sql = preg_replace("/[\n\t]+/", " ", $sql);
  • 更改 '_wpg' 和 'DERP' 到CAST('_wpg' AS BINARY(255))
  • 更改 '_wpg' 和 'DERP' 到BINARY '_wpg'

我使用MediaWiki DatabaseMysql類來執行查詢和讀取行,但這是一個非常簡單的抽象,我確信它不是問題(請參閱belo W)。

SELECT 
    prop.name AS prop_name, prop.count AS prop_count, prop.type AS prop_type, 
    val.value AS val_value, val.unit AS val_unit, val.count AS val_count 
FROM 
(
    SELECT 
     s_id, name, type, COUNT(foo.name) AS count 
    FROM ( 
     (
      SELECT 
       cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, spec.value_string AS type 
      FROM `smw_ids` s 
      INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_atts2` a) 
       ON (cl.cl_from = p.page_id AND 
        p.page_title = s2.smw_title AND 
        s2.smw_id = a.s_id AND 
        a.p_id = s.smw_id) 
      LEFT JOIN `smw_spec2` spec ON s.smw_id = spec.s_id 
     ) 

     UNION ALL   

     (
      SELECT 
       cl.cl_to AS cat_name, s.smw_id AS s_id, s.smw_sortkey AS name, '_wpg' AS type 
      FROM `smw_ids` s 
      INNER JOIN (`categorylinks` cl, `page` p, `smw_ids` s2, `smw_rels2` a) 
       ON (cl.cl_from = p.page_id AND 
        p.page_title = s2.smw_title AND 
        s2.smw_id = a.s_id AND 
        a.p_id = s.smw_id) 
     ) 
    ) AS foo 

    WHERE foo.cat_name = 'Derp' 
    GROUP BY name 
    ORDER BY count DESC 
    LIMIT 10 
) AS prop 


INNER JOIN 

(
    SELECT 
     bar.p_id AS p_id, bar.value AS value, bar.unit AS unit, COUNT(bar.value) AS count, 
     IF(@prev != p_id, @rownum := 1, @rownum := @rownum+1) AS rank, 
     @prev := p_id 
    FROM ( 
     (SELECT a.p_id AS p_id, a.value_xsd AS value, a.value_unit AS unit FROM `smw_atts2` a) 
      UNION ALL 
     (SELECT r.p_id AS p_id, s.smw_sortkey AS value, NULL AS unit 
      FROM `smw_rels2` r INNER JOIN `smw_ids` s ON r.o_id = s.smw_id) 
    ) AS bar 

    GROUP BY value, unit 
    ORDER BY count DESC 

) AS val  
ON prop.s_id = val.p_id 

WHERE val.rank <= 50 

ORDER BY prop_count DESC, prop_name, val_count DESC, val_value 

編輯:以下測試腳本不輸出任何內容。 query.sql完全包含上面的查詢,在MediaWiki的數據庫類的mysql_query()調用之前立即寫入文件。

$db = mysql_connect('localhost', 'root', ''); 
mysql_select_db('mediawiki', $db); 

$res = mysql_query(file_get_contents("query.sql"), $db); 

while ($row = mysql_fetch_assoc($res)) { 
    var_dump($row); 
} 
echo mysql_error($db); 

編輯:我進口龐大的數據庫轉儲和事後,當我裝PHP頁面,有一個明顯的等待,似乎表明該查詢正在運行,但仍然沒有結果發現。我結束了重新查詢,我不再有這個問題。

+1

如果執行查詢時它可能不是問題。請發佈PHP代碼。 – Cfreak 2011-02-16 23:11:22

回答

0

試試這個能夠更好地檢測和報告錯誤:

$db = mysql_connect('localhost', 'root', ''); 
mysql_select_db('mediawiki', $db); 

$res = mysql_query(file_get_contents("query.sql"), $db); 
if (!$res) { 
    print "SQL Error ".mysql_errno().":".mysql_error().", from query: '".file_get_contents("query.sql")."'"; 
} else { 
    while ($row = mysql_fetch_assoc($res)) { 
    var_dump($row); 
    } 
}