Jump to content

If statement getting empty query error


Usarjjaco

Recommended Posts

Hey Guys,I'm exhausted; so I'm hoping someone on here can help me get what I'm missing.using something along these lines on a page:IF($variable1<20){// This stuff}IF($variable1>=20 AND $variable2=='N' AND $variable3=='N'){// Stuff In This situation}IF($variable1>=20 AND $variable2!='N' AND $variable3=='N'){// Stuff in this one}IF($variable1>=20 AND $variable2=='N' AND $variable3!='N'){//Stuff in this one.}... Here's the problem. I keep getting a empty query result; and I'm not sure why. I have it set up so that it checks if they are or are not equal... maybe it's something to do with the amount of if statements or maybe I'm supposed to structure the AND differently.. help would be greatly appreciated. Thanks guys.JJ

Link to comment
Share on other sites

Can you show us the query? Also, posting your complete actual code helps. The "empty query" error doesn't have anything to do with conditions, it just means you've called mysql_query() with an empty string for the first parameter.Anyway, for this sort of condition it's far better to use nested if statements.

if ($variable1 < 20) {	// This stuff} else {	if ($variable2 == 'N') {		if ($variable3 == 'N') {			// Stuff In This situation		} else {			//Stuff in this one.		}				} else {		if ($variable3 == 'N') {			// Stuff in this one		}	}}

Note you will not get anything done if $variable1 >= 20 and $variable2 != 'N' and $variable3 != 'N'.

Link to comment
Share on other sites

Can you show us the query? Also, posting your complete actual code helps. The "empty query" error doesn't have anything to do with conditions, it just means you've called mysql_query() with an empty string for the first parameter.Anyway, for this sort of condition it's far better to use nested if statements.
<?php$zombiedead=mysql_query("SELECT * FROM zombies WHERE building=2");$zombied=mysql_fetch_assoc($zombiedead);$zlife=$zombied['dead'];$npctaken=mysql_query("SELECT * FROM npcs WHERE building=2");$npctake=mysql_fetch_assoc($npctaken);$taken=$npctake['taken'];if($zlife=='Y' AND $taken=='N'){echo "<p><font color=lightgray>You come upon a small single story library and you hear noises. A young woman bolts out the frontdoor and yells to you 'Theres one of those things in there!'She appears shaken but un-infected. You can</font></P>";echo "<p><font color=lightgray>Ask her to join your group and go inside to attackthe zombie.</font></p>";echo "<form action='building2joinattack.php' method='post'>";echo "<input type='submit' value='Join & Attack' />";echo "</form>";echo "<br>";echo "<p><font color=lightgray>Attack The Woman</font></p>";echo "<form action='building2fightnpc.php' method='post'>";echo "<input type='submit' value='Attack The Woman' />";echo "</form>";}if($zlife!='N' AND $taken=='N'){echo "<p><font color=lightgray>You come upon a small single story library and you hear noise. A young woman walks out and says 'Thank goodness!I've been stranded here since my car broke down!' She appears to be healthy.</font></p>";echo "<p><font color=lightgray>Ask her to join your group.</font></p>";echo "<form action='building2joinattack.php' method='post'>";echo "<input type='submit' value='Join' />";echo "</form>";echo "<br>";echo "<p><font color='lightgray'>Attack The Woman</font></p>";echo "<form action='building2fightnpc.php' method='post'>";echo "<input type='submit' value='Attack The Woman' />";echo "</form>";}if($zlife=='Y' AND $taken=='Y'){echo "<p><font color='lightgray'>You come upon a small single story library. You see no onearound but you hear noise coming from inside.</font></p>";echo "<p><font color='lightgray'>Investigate</font></p>";echo "<form action='building2joinattack.php' method='post'>";}if($zlife=='N' AND $taken=='Y'){echo "You find the building is empty.";}?>

Also; what is the advantage of the nested statement? Could I make it possible to include the $variable1 >= 20 and $variable2 != 'N' and $variable3 != 'N'in the nested hierarchy?

Link to comment
Share on other sites

Using nested conditionals makes it easier to read. And of course, all possibilities can be included.

if ($variable1 < 20) {	// This stuff} else {	if ($variable2 == 'N') {		if ($variable3 == 'N') {			// Stuff In This situation		} else {			//Stuff in this one.		}				} else {		if ($variable3 == 'N') {			// Stuff in this one		} else {			//the other condition	}}

What is the actual error you are getting?

Link to comment
Share on other sites

Using nested conditionals makes it easier to read. And of course, all possibilities can be included.
if ($variable1 < 20) {	// This stuff} else {	if ($variable2 == 'N') {		if ($variable3 == 'N') {			// Stuff In This situation		} else {			//Stuff in this one.		}				} else {		if ($variable3 == 'N') {			// Stuff in this one		} else {			//the other condition	}}

What is the actual error you are getting?

I didn't check the actual error; will do that shortly; but It happens after the script runs once; it seems that if the first major IF query is met then the page functions; if not; the error pops up.
Link to comment
Share on other sites

probably because you're query result isn't what you're expecting it to be, and you're conditionals aren't/can't handling it correctly. You should echo that variable at the beginning of the script and within each if statement to make sure that the proper code is being executed based on the actual returned value of the query. All parts of the glorious debugging process. :)

Link to comment
Share on other sites

probably because you're query result isn't what you're expecting it to be, and you're conditionals aren't/can't handling it correctly. You should echo that variable at the beginning of the script and within each if statement to make sure that the proper code is being executed based on the actual returned value of the query. All parts of the glorious debugging process. :)
Ok;The only problem is I don't want the front end user to see anything except the bracketed items. So if I echo the variable then won't they see that on the front-end?
Link to comment
Share on other sites

He means just for debugging purposes, you can remove them once your site goes live.

Link to comment
Share on other sites

He means just for debugging purposes, you can remove them once your site goes live.
Ok,I guess where I'm confused is I thought that if a IF statement was returned false it would just exempt that block of code... is there something I'm missing? I feel like such a peon :)Because the taken variable is either going to be a Y or a N which designates if it should show an NPC character thereAnd the zlife variable is whether there is a "bad guy" at that location, which is also a Y or N and the third variable is whether the player has sufficient time to do the action... So if anyone has any suggestions I'm all ears :)It almost seems like I have the problem with like 1 variable in the if statement returns false and the other 1 or 2 return true...EDIT: I set up the page in a manner that didn't do any IF AND statements and it appears to be working now... Though I'd still like to know why lol
Link to comment
Share on other sites

depends on how you need the order of the logic to run. How did your code end up coming out? And an if statement won't return false just on its own, if the conditions are met, then code within its brackets will execute, if not, it will just run on with the rest of the script. If you attach an else statement onto that if statement, then if that initial if statement conditions are not met, code in the else statement will be executed, and then the rest of the script will execute.

Link to comment
Share on other sites

depends on how you need the order of the logic to run. How did your code end up coming out? And an if statement won't return false just on its own, if the conditions are met, then code within its brackets will execute, if not, it will just run on with the rest of the script. If you attach an else statement onto that if statement, then if that initial if statement conditions are not met, code in the else statement will be executed, and then the rest of the script will execute.
Yeah that's how I thought it worked... I still think it's something to do with running it like IF ($var1=='this' AND $var2=='whatever')I changed it to a nested hierarchy of if / else if statements and it works now ... so I'll just try to make them all work with this method from now on.It almost seemed like inputting more than one condition with 'IF' made it return the query empty error if there was any value other than the one specified by the IF statement. Thanks for the help guys!
Link to comment
Share on other sites

if you only use if statements, and more then one of them are true, then all the one's that meet the right conditions will have their code executed, regardless of whether you wanted only one of them to be true, thus the effect of a later one could overwrite the first one, giving you an unexpected result. In order to funnel logic, you need to use clear and concise if/else statements, as you have found out.

Link to comment
Share on other sites

also, in your first post, you used a couple of = signs in your code, when they should be ==. That's why debugging is key, to make sure your conditions are running for the right reasons. Usually I start the beginning of each function or block of code with a statement saying the name of the function that is running, and maybe why it should be running, like because x < 10 or something like that. Then I print my variables to see if they're right, and then one more time after all the calculations. And of course, whenever you get data passed from another function, or from a query string, etc, the first thing you should is print it out to make sure the whole process is even starting with values you expect.

Link to comment
Share on other sites

also, in your first post, you used a couple of = signs in your code, when they should be ==. That's why debugging is key, to make sure your conditions are running for the right reasons. Usually I start the beginning of each function or block of code with a statement saying the name of the function that is running, and maybe why it should be running, like because x < 10 or something like that. Then I print my variables to see if they're right, and then one more time after all the calculations. And of course, whenever you get data passed from another function, or from a query string, etc, the first thing you should is print it out to make sure the whole process is even starting with values you expect.
All good advice, thanks for all your help :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...