I am using this function to list items from table helps depending on parameters: (I included most of code but you can really just focus on the mysql querys)
function list_helps($by,$value,$page = -1,$ipp = 20){
/* Yes I concatenate querys... so i use some variables to help it a bit */
$sql = 'SELECT helps.* FROM helps ';
$where = ''; $orderBy = ''; $in = ''; $join = ''; $limit = ''; $resultitems = ''; $header = '';
if($page > 0) $limit = 'LIMIT '.$page*$ipp.', '.($page+1)*$ipp; else $limit = 'LIMIT 10';
switch($by){
case 'byuser':
$where = 'WHERE id_user ='.$value;
$orderBy = 'ORDER BY id DESC';
break;
case 'byfriend':
$sql = 'SELECT
h.*,
f.*
FROM (
SELECT
id,
CASE followerid WHEN '.$value.' THEN followingid ELSE followerid END AS friend_id
FROM friends
WHERE acepted = 1
AND (followerid = '.$value.' OR followingid = '.$value.')
) AS f
INNER JOIN helps AS h ON h.id_user = f.friend_id
ORDER BY h.id DESC';
break;
default:
break;
}
$sql .= $where.' '.$orderBy.' '.$limit;
$res = cache_query($sql,'',60*60*5);
/* checks in cache first, if not; executes query.. next code it's to render content */
}
There are more cases but lets focus on those two.
They both serve the ‘helps’ that are being requested (if user, or if users’s friend); The problem is when i am trying to retrieve the comments of those (like if it where a blog post); which I am doing like so:
res = cache_query('SELECT help_replies.content, help_replies.date,
help_replies.offers, help_replies.accepted, help_replies.id_responds,
usuarios.first_name, usuarios.last_name, usuarios.avatar,usuarios.id
FROM help_replies left join usuarios
ON help_replies.id_user = usuarios.id
WHERE help_replies.id_responds = '.$this->id.'
ORDER BY help_replies.id ASC', '', 30);
foreach($res as $obj) {
/* $obj['id_responds'] */
}
}
The thing is that the help_replies.id_responds makes reference to the item, so when the case is ‘byuser’ is fine; but when the case is ‘byfriend’ this field is lost (so is showing always the same comments)
Conclusion:
- byuser -> helps fine & comments fine
- byfriend -> helps fine (which is wierd) & comments wrong
Any Idea why $this->id is not as expected from the (JOINED) query?
-EDIT-
$this->id comes from this construct method (i ommited many atributes)
function __construct($item) {
$this->id = $item['id'];
$this->id_user = $item['id_user'];
..
}
And $item is one iteration of the foreach (so its the returned row converted to array)
Muchas Gracias
Your
byfriendsquery executed byfunction list_helps(...)appears to return a field labelledidtwice; once for yourhelpstable and then again forfthe alias of thefriendstable, as part of the sub-query.I can imagine that if
$this->idcontains a friends id and not the helps id then your comments query (help_replies) will return the wrong results.So perhaps changing your sub-query with an alias for the friends id as shown ie
id AS friendsId,everything will work as expected?I’ve got into the habit of naming my table identifier field
<tablename>Idfor this very reason and saves having to use alias everywhere.