sintax63 Posted February 10, 2010 Share Posted February 10, 2010 I need this for two separate page: a login screen and just a general page. I am not sure if this is possible, but I want to run a variable (in this case "login") and if it can't find any matches from my first table, I want it to try a second table before it fails out. My code thus far: $query="SELECT * FROM table1 WHERE login='$id'"; $result=mysql_query($query); if ($result) { while( $row = mysql_fetch_assoc( $result ) ) { $id = $row["id"]; $login = $row["login"]; $pass = $row["pass"]; $email = $row["email"]; } } else { $query="SELECT * FROM table2 WHERE login='$id'"; $result=mysql_query($query); while( $row = mysql_fetch_assoc( $result ) ) { $id = $row["id"]; $login = $row["login"]; $pass = $row["pass"]; $email = $row["email"]; } } Is this a possible thing to do and am I on the right track? Quote Link to comment https://forums.phpfreaks.com/topic/191612-query-one-table-for-data-if-empty-query-second-table-is-this-possible/ Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 And where is the problem? How the code acts? does it give eny erros or what? Quote Link to comment https://forums.phpfreaks.com/topic/191612-query-one-table-for-data-if-empty-query-second-table-is-this-possible/#findComment-1010039 Share on other sites More sharing options...
sintax63 Posted February 10, 2010 Author Share Posted February 10, 2010 It will return data from the first table, but not from the second. Meaning I enter an ID that isn't in the first table. I want it to realize that it's not there and proceed to try the second table. Instead it just returns the page with empty variables. I have tried tweaking the code a bit to what is below... same results though. $query = "SELECT * FROM table1 WHERE login='$id'"; $result = mysql_query($query); if (empty($result)) { $query = "SELECT * FROM table2 WHERE login='$id'"; $result = mysql_query($query); while( $row = mysql_fetch_assoc( $result ) ) { $id = $row["id"]; $login = $row["login"]; $pass = $row["pass"]; $email = $row["email"]; echo "Table 2"; } } else { while( $row = mysql_fetch_assoc( $result ) ) { $id = $row["id"]; $login = $row["login"]; $pass = $row["pass"]; $email = $row["email"]; echo "Table 1"; } } I added those echo statements in there just so I could see what was happening. It pulls correctly from table 1 but doesn't try table 2 if it can't find the username / ID. Quote Link to comment https://forums.phpfreaks.com/topic/191612-query-one-table-for-data-if-empty-query-second-table-is-this-possible/#findComment-1010042 Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 try this if(!mysql_num_rows()) { //table2 } else { } Quote Link to comment https://forums.phpfreaks.com/topic/191612-query-one-table-for-data-if-empty-query-second-table-is-this-possible/#findComment-1010055 Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 if(!mysql_num_rows($result))* Edit: double Enter... Quote Link to comment https://forums.phpfreaks.com/topic/191612-query-one-table-for-data-if-empty-query-second-table-is-this-possible/#findComment-1010057 Share on other sites More sharing options...
sintax63 Posted February 10, 2010 Author Share Posted February 10, 2010 The line works perfectly! Thank you so much for the quick assistance, sader. Quote Link to comment https://forums.phpfreaks.com/topic/191612-query-one-table-for-data-if-empty-query-second-table-is-this-possible/#findComment-1010058 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.