-3

I am trying to query 3 tables but it is returning an error. I want the code to check the 3 tables whether the user is registered in one of these tables if he tries to log in. Please help me I'm just a php beginner.

I get the following error:

Warning: mysql_result() expects parameter 1 to be resource, boolean given in

This is my code:

//check user if exist    

function user_exists($username){

    $username = sanitize($username);    

return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM 

`users`,`admin`,`guest` WHERE `username` = '$username'"), 0) == 1) ? true : false;
Funk Forty Niner
  • 74,372
  • 15
  • 66
  • 132
claidesky
  • 1
  • 1

1 Answers1

0

It appears that you are attempting to grab from 3 different tables using a single SELECT, which cannot work.

I'd suggest trying to use a JOIN and if you are unfamiliar (and in worst case), using 3 separate SELECT queries to achieve your end goal.

Derek Pollard
  • 6,630
  • 6
  • 37
  • 54
  • Hi derek , i have a mysql connection sorry i just didnt include it in my post . the code that i posted is running when i select only 1 table but if i try to select 2 or 3 tables its returns an error – claidesky Jan 07 '16 at 17:59
  • Ahh, then in that case, you'd need to either write 3 different queries or use a `join` – Derek Pollard Jan 07 '16 at 18:00
  • 1
    I doubt this is a variable scope issue. `mysql_` will keep a connection if it was first established. Seeing their `mysqli_` tag, could suggest that they're using `mysqli_` to connect with (or PDO), and that is unknownst to us. – Funk Forty Niner Jan 07 '16 at 18:04
  • @Fred-ii- i'd agree, however OP said it works with 1 table, which leads me to believe that he/she is attempting to use a single select to grab from 3 different tables, which we know cannot work – Derek Pollard Jan 07 '16 at 18:06
  • ah ok. Yeah, they're doing it wrong then. Some type of JOIN would be the way to go here. Their query's off for sure. If tables have nothing in common, then they'll need to use something else. – Funk Forty Niner Jan 07 '16 at 18:08
  • Updated my answer accordingly :-) – Derek Pollard Jan 07 '16 at 18:09