我有两张桌子(桌子a和桌子b)和一个联合体。
我如何检查记录是否为表。
我试过,但没成功:

$res=mysql_query("(SELECT id, added FROM table_a WHERE user_id = '".$uid."')
UNION
(SELECT id as comments, added FROM table_b WHERE user_id = '".$uid."') ORDER BY added DESC LIMIT 50");

if(!empty($rows["comments"])) // not working

最佳答案

它不起作用,因为您忘记读取结果集:

while ($row = mysql_fetch_assoc($res)) {
// here you read $rows["comments"] etc
...

}

重要:
请不要使用mysql_*函数,它已被弃用(请参阅red box),并且易受sql注入攻击。使用PDOMySQLi
更新:
如果要知道结果“来自”哪个表,可以将查询更改为:
(SELECT id, added, 'A' as came_from FROM table_a WHERE user_id = '".$uid."')
UNION
(SELECT id as comments, added, 'B' as came_from FROM table_b WHERE user_id = '".$uid."') ORDER BY added DESC LIMIT 50

然后检查值:$rows["came_from "]

10-08 20:20