How do I merge two mysql results?

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

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

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

Jay

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

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

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

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!

adelinewss
New php-forum User
New php-forum User
Posts: 4
Joined: Thu Oct 24, 2002 6:20 pm
Location: Malaysia
Contact:

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:

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

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...?

DoppyNL

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


Return to “PHP coding => General”

Who is online

Users browsing this forum: buckibooster, Google [Bot] and 4 guests