douxi2011 2016-01-12 17:35
浏览 28
已采纳

使用从2个数据库中选择,然后将结果放入一个php do函数中

I'm trying to select data from 2 databases then use php do I just want to give an example that will explain every thing:

php code:

  • first database name is comments.

$colname_Program = "Any title from programs table :)"; mysql_select_db($database_Comments, $Comments); $comments = mysql_query('SELECT * FROM programs WHERE title = "$colname_Program"', $Comments) or die(mysql_error()); $row_comments = mysql_fetch_assoc($comments); $totalRows_comments = mysql_num_rows($comments); $commentuserid = $row_comments['userid'];

  • "userid" is the id of the user which i will use in my second select from another database"

All what im trying to do from second select is to bring user information from second database using his id.

  • Second database name is naitreno.

mysql_select_db($database_NaiTreNo, $NaiTreNo); $commentsuser = mysql_query('SELECT * FROM users WHERE id = "$commentsuserid"', $Comments) or die(mysql_error()); $row_commentsuser = mysql_fetch_assoc($commentsuser); $username = $row_commentsuser['UserName'];

html and php code bundle now:

<?php do { ?> <div class="comment-text"><?php echo $username;?></div>> <?php } while ($row_comments = mysql_fetch_assoc($comments)); }?>

The code is not working :O what have I done wrong or what i have to do its just showing the first username in html<div class="comment-text"></div>.

  • 写回答

2条回答 默认 最新

  • douyao7390 2016-01-12 18:47
    关注

    You need to union. UNION is used to combine the result from multiple SELECT statements into a single result set

    http://dev.mysql.com/doc/refman/5.7/en/union.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。