因此,我已经调试了这个超长查询。我最初收到的错误是s.name列和所有其他JOIN列不在“字段列表”中,因此经过一番谷歌搜索后,我能够通过将它们放在双引号中来解决该错误。因此,现在查询返回的资源没有错误,但是资源为空。

现在,我在语句后从回声中得到的是“资源ID#4警告:implode():无效的参数在第143 0行的/var/www/beta/index.php中传递”

这是查询和周围的功能:

<?php /* other functions preceding */ $result = mysql_query("SELECT * FROM users WHERE uid=" . $_SESSION['uid'] . "");
    $cur_user = mysql_fetch_array($result);
    $ufriends = explode(';', $cur_user['friends']);
    $ufsql = trim(implode(',',$ufriends),',');
    $uevents = explode(';', $cur_user['events']);
    $uesql = trim(implode(',',$uevents),',');
    $urequests = explode(';', $cur_user['requests']);
    $ursql = trim(implode(',',$urequests),',');
    if (!empty($ufriends)) {
        $time1 = microtime();
        $megaresult = mysql_query("
            ( SELECT \"s.name\" AS source_name, NULL AS target_name, recent_updates.*, 1 AS ORD FROM recent_updates
                INNER JOIN users AS s ON (\"s.uid\"=recent_updates.source_id)
                WHERE update_type='10'
                LIMIT 1
            )
            UNION
            ( SELECT \"s.name\" AS source_name, NULL AS target_name, recent_updates.*, 2 as ORD FROM recent_updates
                INNER JOIN users AS su ON (\"s.uid\"=recent_updates.source_id)
                WHERE update_type='10'
                LIMIT 1,9
            )
            UNION
            ( SELECT \"s.name\" AS source_name, \"t.name\" AS target_name, recent_updates.*, 2 AS ORD FROM recent_updates
                INNER JOIN users AS s ON (\"s.uid\"=recent_updates.source_id)
                INNER JOIN users AS t ON (\"t.uid\"=recent_updates.target_id)
                WHERE update_type='2'
                AND
                    ( target_id IN (" . $ufsql . ") )
                AND
                    ( source_id IN (" . $ufsql . ") )
                OR
                (
                    ( target_id IN (" . $cur_user['uid'] . ") )
                AND
                    ( source_id IN (" . $ufsql . ") )
                )
                OR
                (
                    ( target_id IN (" . $ufsql . ") )
                AND
                    ( source_id IN (" . $cur_user['uid'] . ") )
                )
                LIMIT 0,10
            )
            UNION
            ( SELECT \"s.name\" AS source_name, \"t.name\" AS target_name, recent_updates.*, 2 AS ORD FROM recent_updates
                INNER JOIN users AS s ON (\"s.uid\"=recent_updates.source_id)
                INNER JOIN users AS t ON (\"t.uid\"=recent_updates.target_id)
                WHERE update_type='4'
                AND
                (
                    ( target_id IN (" . $ufsql . ") )
                AND
                    ( source_id IN (" . $ufsql . ") )
                )
                OR
                (
                    ( target_id IN (" . $cur_user['uid'] . ") )
                AND
                    ( source_id IN (" . $ufsql . ") )
                )
                OR
                (
                    ( target_id IN (" . $ufsql . ") )
                AND
                    ( source_id IN (" . $cur_user['uid'] . ") )
                )
                LIMIT 0,10
            )
            UNION
            ( SELECT \"s.name\" AS source_name, \"t.name\" AS target_name, recent_updates.*, 2 AS ORD FROM recent_updates
                INNER JOIN users AS s ON (\"s.uid\"=recent_updates.source_id)
                INNER JOIN events AS t ON (\"t.id\"=recent_updates.target_id)
                WHERE update_type='3'
                AND
                target_id IN (" . $uesql . ")
                LIMIT 0,10
            )
            UNION
            ( SELECT \"s.name\" AS source_name, \"t.name\" AS target_name, recent_updates.*, 2 AS ORD FROM recent_updates
                INNER JOIN users AS s ON (\"s.uid\"=recent_updates.source_id)
                INNER JOIN events AS t ON (\"t.id\"=recent_updates.target_id)
                WHERE update_type='3'
                AND
                target_id IN (" . $ursql . ")
                LIMIT 0,10
            )
            UNION
            ( SELECT \"s.name\" AS source_name, \"t.name\" AS target_name, recent_updates.*, 2 AS ORD FROM recent_updates
                INNER JOIN users AS s ON (\"s.uid\"=recent_updates.source_id)
                INNER JOIN events AS t ON (\"t.id\"=recent_updates.target_id)
                WHERE update_type='5'
                AND
                target_id IN (" . $ursql . ")
                LIMIT 0,10
            )
            UNION
            ( SELECT \"s.name\" AS source_name, \"t.name\" AS target_name, recent_updates.*, 2 AS ORD FROM recent_updates
                INNER JOIN users AS s ON (\"s.uid\"=recent_updates.source_id)
                INNER JOIN events AS t ON (\"t.id\"=recent_updates.target_id)
                WHERE update_type='11'
                AND
                (
                    target_id IN (" . $uesql . ")
                OR
                    target_id IN (" . $ursql . ")
                )
                AND
                (
                    source_id IN (" . $ufsql . ")
                )
                LIMIT 0,10
            )
            UNION
            ( SELECT NULL AS source_name, \"t.name\" AS target_name, recent_updates.*, 2 AS ORD FROM recent_updates
                INNER JOIN events AS t ON (\"t.id\"=recent_updates.target_id)
                WHERE public != 0 LIMIT 0,10
            )
            ORDER BY ORD ASC, time_un DESC");
        echo $megaresult;
        $feed = mysql_fetch_array($megaresult);
        echo mysql_error();
        $time2 = microtime();
        echo implode('##', $feed);
        echo mysql_num_rows($megaresult);
        echo mysql_info($con);
/* brackets closed, etc */ ?>


我觉得这与将别名列放在双引号中有关。我有此查询的先前版本,其中不包含任何运行正常的JOIN部分,我将其上传到pastebin:http://pastebin.com/upifa7VJ

编辑:

好吧,这令人尴尬...我正确地怀疑引号是引起问题的原因(感谢@andrewtweber!),但是我没有引号的错误是由于第二个SELECT语句中的错字,INNER JOIN users AS su代替INNER JOIN用户。感谢大家的帮助!

最佳答案

双引号表示您选择的是字符串“ s.name”,而不是列s.name

您可能想做的是使用反引号`。反引号允许您将保留的MySQL关键字用作列名,例如

SELECT `table`.`name` FROM `table` ORDER BY `order` ASC

关于php - MySQL查询返回无效资源但没有错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11275643/

10-11 21:48
查看更多