Web Hosting Talk







View Full Version : Load Two MySQL Tables at Once


epic1231
10-09-2009, 12:06 PM
Hey everyone, I found a thread on here via Google about loading two MySQL tables at once.. I am in need of doing that but with checking one of those tables also.

My game allows users to become friends with certain members, once they are friends they are added into my database (crew_log)

In another section I would like the users to be able to fight against all the users who are registered with the game but not include their friends/crew in the list

Right now I am using this


if( $pirate->level==1 OR $pirate->level==2 ) $lowerLevel = 1;
else $lowerLevel = ($pirate->level - 20);

$upperLevel = ($pirate->level + 20);

$res = query("SELECT * FROM `appusers` WHERE `uLevel`>=$lowerLevel AND `uLevel`<=$upperLevel AND `userid`!=$user ORDER BY RAND() LIMIT 0,30");


appusers is a table where users are stored.

I want to pull that info but the crew_log holds the information of who is friends, is there anyway of combining them so that if two users are friends they will not show up in the battle list?

Crew Log holds just logId userid crewid (userID being the the players ID and the crewID being the friends ID)

Example of what I mean

Player 1 and Player 2 become friends, Player 1 decides to go battle and loads the list of users and Player 2 does not show up in that list because they are crew together

I was thinking of something like this which kind of works but I am not sure how to properly phrase the query


if( !empty($_GET[id]) ) {
$theUser = $_GET[id];
}
else {
$theUser = $user;
}

if( !check_if_crew( $user, $theUser ) ) {
echo 'not sure what query to replace this echo with';
} else {


Any help would be very much appreciated, been stuck on this for awhile now and can't figure it out

larwilliams
10-09-2009, 03:55 PM
SELECT * FROM appusers LEFT JOIN crew_log ON appusers.id = crew_log.userID WHERE appusers.id != crew_log.userID

Not tested, but should get you a list of all users that are not their friends.

larwilliams
10-09-2009, 03:58 PM
You can also use the following query:

SELECT * FROM appusers, crew_log WHERE appusers.id != crew_log.userID

larwilliams
10-09-2009, 04:41 PM
You may also want to make sure you sanitize your user input from $_GET and $_POST variables, as you could get an SQL injection otherwise.