Page 1 of 1

Help with Mysql Select 2 tables

Posted: Wed Apr 11, 2012 9:19 pm
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.";
}

Re: Help with Mysql Select 2 tables

Posted: Wed Apr 11, 2012 9:25 pm
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}'";

Re: Help with Mysql Select 2 tables

Posted: Wed Apr 11, 2012 9:42 pm
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 ;
}

Re: Help with Mysql Select 2 tables

Posted: Thu Apr 12, 2012 5:37 am
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);
}