MySQL查询返回mysql中的行但在PHP中为空集

时间:2011-02-16 23:05:18

标签: php mysql mediawiki

以下MySQL查询在PHP中运行且没有错误,但结果集为空。直接将查询字符串输出到文件并使用'source [filename]'在MySQL客户端中运行查询会返回几行结果,如预期的那样。

是否存在会导致此查询无法与PHP一起使用的内容? categorylinks.cl_to和smw_spec2.value_string都是varbinary(255)。显示create table表示engine = InnoDB,默认charset = binary。

我尝试过的事情没有成功:

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

我正在使用MediaWiki DatabaseMysql类来执行查询和获取行,但它是一个非常薄的抽象,我确定这不是问题(见下文)。

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页面时,有一个明显的等待似乎表明查询正在运行,但仍然没有显示结果。我最终重新编写了查询,我不再有这个问题了。

2 个答案:

答案 0 :(得分: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);
  }
}

答案 1 :(得分:0)

mysql_connect

之后尝试此操作
mysql_query('SET NAMES utf8;');