So I’ve got this super long query that I’ve been debugging. I originally received an error that column s.name and all the other JOIN columns are not in the "field list", so after some googling I was able to fix that error by putting them in double quotes. So now the query returns a resource without an error, but the resource is empty.
Now what I get from the echos after the statement is "Resource id #4 Warning: implode(): Invalid arguments passed in /var/www/beta/index.php on line 143 0"
Here is the query and surrounding functions:
<?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 */ ?>
I have a feeling it has something to do with putting the aliased columns in double quotes. I had a previous version of this query which didn’t include any of the JOIN parts which worked perfectly fine, I uploaded it to pastebin: http://pastebin.com/upifa7VJ
EDIT:
Well this is embarrassing… I was right to suspect that the quotes were causing the problem (thanks @andrewtweber!), but the error I was getting without the quotes was due to a typo in the second SELECT statement, INNER JOIN users AS su instead of INNER JOIN users as s. Thanks for the help everyone!
The double quotes mean you are selecting the string “s.name” and not the column
s.name.What you probably meant to do was use a backtick `. The backtick allows you to use reserved MySQL keywords as column names, e.g.