Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

How do I merge two mysql results?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

How do I merge two mysql results?

Postby ShenaCat » Wed Oct 23, 2002 1:30 pm

So I have two SQL statements in php:

$sql = "SELECT * FROM users WHERE location='$lid'";
$sql2 = "SELECT * FROM admin WHERE location='$lid'";

$result = mysql_query($sql);
$result2 = mysql_query($sql2);

now I want to merge $result and $result2 into one variable so I can work with them together... how can I do that? Are they just regular arrays..? if I merged them together could I then do a mysql_num_rows($merged_var) function on it...?

Thanks
User avatar
ShenaCat
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Oct 21, 2002 1:16 pm
Location: Seattle, WA

Postby Jay » Thu Oct 24, 2002 1:53 am

Output them into an array first, and then manipulate them from there!
Jay
 

Postby ShenaCat » Thu Oct 24, 2002 2:36 pm

The problem is, that'd be a hell of an array :)

I've decided to use a temporary table and just output what I want from each table into this temporary table and then pull the data from there... it seemed the most expedient method until UNION in mySQL 4.0.0!
User avatar
ShenaCat
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Oct 21, 2002 1:16 pm
Location: Seattle, WA

Postby adelinewss » Thu Oct 24, 2002 6:47 pm

Can tried with $sql = "select * from users u, admin a where location='$lid' and u.id=a.id";

(u.id and a.id is the key to link the two tables)

* Please note that there must be a key to link the two tables or else the combined sql will not work. Good Luck! :wink:
adelinewss
New php-forum User
New php-forum User
 
Posts: 4
Joined: Thu Oct 24, 2002 6:20 pm
Location: Malaysia

Postby ShenaCat » Thu Oct 24, 2002 6:51 pm

I tried that already and it doesn't work, because it puts the results all in one row instead of in multiple rows...?
User avatar
ShenaCat
New php-forum User
New php-forum User
 
Posts: 9
Joined: Mon Oct 21, 2002 1:16 pm
Location: Seattle, WA

Postby DoppyNL » Fri Oct 25, 2002 2:25 am

IF in the table `user` users are stored and in the table `admin` administrators, you might want to rethink you're databasestructure...

Personally, I store all users of my site in one table, and use fields to give them certain rights or not.

Greetz Daan
DoppyNL
 


Return to PHP coding => General

Who is online

Users browsing this forum: Baidu [Spider], Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.