MySQL查询返回无效资源但没有错误

时间:2012-06-30 16:17:09

标签: php mysql inner-join

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

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

以下是查询和周围函数:

<?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用 s 加入用户AS su 而不是INNER JOIN用户 s 。感谢大家的帮助!

2 个答案:

答案 0 :(得分:2)

由于您的问题是拼写错误,让我借此机会向您介绍PDO

你知道每个人都在告诉你关于SQL注入的事情,它有多可怕吗?那么,通过正确的练习,PDO可以为您解决这个问题。

这很糟糕:

$result = mysql_query("SELECT * FROM users WHERE uid=" . $uid . "");

这更糟糕!

$result = mysql_query("SELECT * FROM users WHERE uid=" . $_GET['uid'] . "");

现在让我们用PDO

执行代码的前几行
//Getting the handle that will allow us to do stuff with the Database
$dbh = new PDO("mysql:host=$hostname;dbname=mysql", $username, $password);
$sql = "SELECT * FROM users WHERE uid= :uid"
//Now we prepare our query, send it to the database server and get it ready
$stmt = $dbh->prepare($sql);
//So now we add the parameters to the query
$stmt->bindParam(':uid', $_SESSION['uid'], PDO::PARAM_INT);
//Now we execute it
$stmt->execute();
//Fetching the result
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
//Let's see our result
print_r($result);

就是这样!你写了几行,但不仅保护了你的数据库,而且你自己有机会提高性能,并对你遵循一些良好的实践原则感到高兴。

您刚刚阅读的内容还不够,请务必查看PDO的文档或至少查看good tutorial

答案 1 :(得分:1)

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

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

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