Help with Mysql Select 2 tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Browny
Forum Newbie
Posts: 4
Joined: Wed Apr 11, 2012 9:16 pm

Help with Mysql Select 2 tables

Post by Browny »

Hey Guys,

Hey Guys,

After searching on google and other forums for solutions for this warning im still having trouble as to why it wont identify if an email already exsists in either of the 2 tables (managers, players).

Warning: mysql_result() expects parameter 1 to be resource, boolean given in /Users/Tim/Sites/DMS/RoutoSMS/functions.php on line 71

Code: Select all

function emailExists($email) {
	$query = mysql_query("SELECT `managers`.`email`, `players`.`email` FROM `managers`, `players` WHERE `email` = '$email'");
	return (mysql_result($query, 0) > 0) ? true : false ;
}

This is the code where it identifies the function:

Code: Select all

if(emailExists($email)) {
	$errors[] = "Email already registered.";
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help with Mysql Select 2 tables

Post by Celauran »

The 'email' in your WHERE clause is ambiguous. Also, why are you selecting email when your input is email? Try something like this:

Code: Select all

$query = "SELECT COUNT(managers.id) AS mCount, COUNT(players.id) AS pCount
          FROM managers, players
          WHERE managers.email = '{$email}'
          OR players.email = '{$email}'";
Browny
Forum Newbie
Posts: 4
Joined: Wed Apr 11, 2012 9:16 pm

Re: Help with Mysql Select 2 tables

Post by Browny »

Thanks for your reply.

In my database, in the 'managers' table I input an email. Although i used your code above and it still registered the email in the 'players' table. Where it should've said 'Email already registered'.

This is the code implemented that you gave me:

Code: Select all

function emailExistsPlayers($email) {
	$query = mysql_query("SELECT COUNT(managers.id) AS mCount, COUNT(players.id) AS pCount
	          FROM managers, players
	          WHERE managers.email = '{$email}'
	          OR players.email = '{$email}'");
	return (mysql_result($query, 0) > 0) ? true : false ;
}
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Help with Mysql Select 2 tables

Post by Celauran »

Your return line is no good.

Code: Select all

function emailExists($email)
{
    $query = "SELECT COUNT(managers.id) AS mCount, COUNT(players.id) AS pCount
              FROM managers, players
              WHERE managers.email = '{$email}'
              OR players.email = '{$email}'";
    list($mCount, $pCount) = mysql_fetch_row(mysql_query($query));
    
    return ($mCount || $pCount);
}
Post Reply