douxin8383 2011-10-28 17:32
浏览 41

如何使用PHP和UNION连接未知数量的mysql数据库?

I have 1 mysql database named A with one table named USERS that holds a users ID, & NAME, & EMAIL.

On the site I am working on I have it set up so that when the user registers it automatically sets up a new seperate mysql database for each user which is named after their ID from the USERS table. So there are an unknown number of databases depending on how many people register but the names of the databases are easy 1D, 2D, 3D, 4D, 5D & so on. On each database I have 2 tables which are also named after the ID number from the USERS table FRIENDS & CARS. So 1FRIENDS, 2FRIENDS, 3FRIENDS, 4FRIENDS & 1CARS, 2CARS, 3CARS, 4CARS & so on. The FRIENDS table allows each user to make a list of other users on the site. The CARS field holds information on the toy cars each user collects. I am trying to write the PHP code to take A users FRIENDS & show all of their friends cars using UNION to connect all of the users friends CARS tables & order them by the date that each user added the info on each car but I can't figure out how to do it.

I can get it to work if I know exactly what friends a user has for example if a person is friends with user 1 & user 3 & I code it like this it will work.

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD) OR die 
('Could not connect to MySQL: ' . mysqli_connect_error() );

$query = "SELECT * FROM 1D.1CARS UNION SELECT * FROM
3D.3CARS   ORDER BY date_entered DESC";


if ($result1 = @mysqli_query($dbc, $query)) {

while ($row = mysqli_fetch_array($result1 )) {

But I won't know which people a user will choose to be friends with ahead of time nor do I want to type all of that out each time so I need to use some sort of variable & a loop or something to take the list each persons is friends with & do the UNION. Any help would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • dongzhuo0895 2011-10-28 17:36
    关注

    On the site I am working on I have it set up so that when the user registers it automatically sets up a new seperate mysql database for each user which is named after their ID from the USERS table. So there are an unknown number of databases depending on how many people register but the names of the databases are easy 1D, 2D, 3D, 4D, 5D & so on

    Stop right there. Don't do that. Terrible, terrible idea. The proof that this is a bad idea is that you now have issues like this one.

    Put user information on the same database on a single table. Differentiate users by their id. In general, it's one app and it looks to me that you need only one database. Don't continue this path. It's wrong.

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)