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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器