简体   繁体   English

php在一个查询中从两个表打印结果

[英]php print result from two table in one query

i fetch two table result in one query. 我在一个查询中获取两个表结果。 now i have in each table one row with name = userid . 现在我在每个表中都有一行name = userid when i print id of table one, this not worked and printed id of table two . 当我打印表一的ID时,这不起作用并打印了表二的ID。 in ie i select * from product and then voteid,userid form voteproduct where userid = $userid ; 在ie中,我从产品中选择* ,然后从voteid,userid选择voteid,userid形式,其中userid = $ userid; now i need print userid from product table but my result print userid from voteproduct . 现在我需要从产品表中打印用户ID,但从表决产品中得到结果打印用户ID。 how to fix this ? 如何解决呢? thanks 谢谢

my code : 我的代码:

$sql = 'SELECT * FROM ' . PREFIX  . '_product,' . PREFIX  . '_voteproduct WHERE ' . PREFIX  . '_voteproduct.voteid = ' . PREFIX  . '_product.id AND ' . PREFIX  . '_voteproduct.userid =  "' . $userid . '" ORDER BY name ' . $pages->limit .' ';

    $db->query($sql) or error ('ERROR', mysql_error ());
          $result = $db->query ( $sql ); 
          if($db->numrows($result)>0){ 
          while ($row = $db->fetcharray($result))
       {

    echo $row['userid']; // PROBELM THIS PRINTED USERID FROM VOTEPRODUCT I NEED PRINT USER ID FROM PRODUCT
       }

EDIT : If I selected * from my product table, realy i need to all. 编辑 :如果我从产品表中选择* ,那么我真的需要所有人。 ( name, price, date, category, ...) (名称,价格,日期,类别等)

select * is a lazy way of doing thigns, especially when it looks like you're only asking for a SINGLE field. select *是一种懒惰的处理方法,尤其是当您看起来只要求输入SINGLE字段时。 You don't run to the store, grab the entire store's shelf stock, take it home, then throw away EVERYTHING except the one candy bar you wanted. 您无需去商店,就可以拿走整个商店的货架库存,将其带回家,然后扔掉所有东西,除了想要的一根糖果吧。

How about 怎么样

SELECT product.userid
FROM ...
WHERE ...

so you get only the exact single field you need? 这样您仅获得所需的确切单个字段?

You can select each userid with AS in order to use them separately like below 您可以使用AS选择每个用户ID,以便分别使用它们,如下所示

$sql = 'SELECT p.userid as pid, vp.userid as vpid, ... FROM ' . PREFIX  . '_product AS p,' . PREFIX  . '_voteproduct AS vp WHERE vp.voteid = p.id AND vp.userid =  "' . $userid . '" ORDER BY name ' . $pages->limit .' ';

And use ; 并使用;

echo $row['pid'];
select p.userid as puserid, v.userid as vuserid ... FROM ' . PREFIX  . '_product p,' . PREFIX  . '_voteproduct v where ...

echo $row['puserid'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM