Jump to content

How to code 2 queries with 'if' statement?


RON_ron

Recommended Posts

I need to check if in databaseA (shown below as applicant) it says "PendingGroupA" if yes echo "something"

and if not to check databaseB (shown below as comp) for the username and password.

Then upon successful username and password entry echo  "message B".

And if username and password fails to echo "message C".

 

How can I write this correctly?

 

$result=mysql_query("SELECT * FROM  comp WHERE username = '$useid' AND pass ='$pass'");
if(mysql_num_rows ($result) == 0) {
$login = "&err=Login Failed. Please retry.";
echo($login);
} elseif {
$row = mysql_fetch_array($result);
$user=$row['user'];
$pass=$row['pass'];
$login = "$user=" . $user . "$pass=" . $pass . "&err=Login Successful.";
echo($login);
} else {
$resultA=mysql_query("SELECT * FROM applicant WHERE username = '$useid' AND statusA ='PendingGroupA'");
$peningGA = "&err=Please be patient!";
echo($peningGA);
}

Link to comment
Share on other sites

You should write in pseudo code first. Basically just the text description of your program flow, in fact you have more or less done it in your post. Once you do this it is eacy to go over the pseudo code with real code. i.e

 

// pseudo code example
1. Check if record exists in table a
2. If it does redirect the user to page 1
3. If not stay on the same page and display an error

 

Here is the code for your post. Fill in the blanks.

<?php
/*
check table applicant for text PendingGroupA
*/
$result = mysql_query("SELECT id FROM applicant WHERE username='' AND statusA='PendingGroupA' LIMIT 1");
if(mysql_num_rows($result)) {
/*
record exists
*/
print 'Message A: Record exists in applicant';
}
else {
/*
no record exists, check comp table for user
*/
$result = mysql_query("SELECT id FROM comp WHERE username='' AND pass='' LIMIT 1");
if(mysql_num_rows($result)) {
	/*
	record exists
	*/			
	print 'Message B: Record exists in comp';
}
else {
	/*
	no record exists
	*/
	print 'Message C: No record exists in comp';
}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.