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 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀