Jump to content


if statement


6 replies to this topic

#1 Hooch

    Young Padawan

  • Members
  • Pip
  • 158 posts
  • Location:Brockville, Ontario

Posted 22 August 2006 - 11:15 PM

Hello all.
I'm missing something here.

The code works if there is a result from the query. ie some users
status is equal to NULL.
But if the query is 0 the "if statement" doesn't click in.
<table width="200" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#999999">
  <tr>
	<td height="25" bgcolor="#000000">
	<div align="center" class="green-18">
	<strong>Members that need thier status set</strong>
	</div>
	</td>
<? include("../includes/db.php"); 
$NULL = mysql_query("SELECT * from users WHERE status = 'NULL' ");	 
if ($NULL >= 1) {
while ($r = mysql_fetch_array($NULL)) 
{ ?>
  <tr>
	<td align="left">
	<a href="admin2.php?user=<? echo $r[username]; ?>" target="_self"class="link"><? echo $r[firstname]; ?> <? echo $r[lastname]; ?></a>
	</td>	
  </tr>
<? } 
   } else { ?>
  <tr>
	<td>
	<div align="center">
	<strong>All caught up here :)</strong>
	</div>
	</td>
  </tr>
</table>
 <? } ?>
</table>

I may have this
if ($NULL >= 1)
wrong too. I am only guessing

Any help would be great.
Thank you.
Hooch

#2 venomsnake

    Jedi In Training

  • Members
  • PipPip
  • 481 posts
  • Gender:Male

Posted 22 August 2006 - 11:40 PM

the if statment wont kick in because of the query, if you have where status = 'NULL', it wont select any rows except the rows that are null, so it wont pull any rows that has something in the status field.

if you take out the status = 'null' part, then the if statment will work, and when the field is empty, the else part will go instead.

edit: i just thought of something you can add to it to make it better,

$NULL = mysql_query("SELECT * from users");	
if ($NULL >= 1) {
stuff here
} elseif ($NULL = NULL) {
stuff here
} else {
more stuff
}

if you do it like that, it will show your code in the first part if its a number greater to or equal to 1, else if its null it doesnt something else, then if it has text or a number less then 1, it does something else again.

Edited by venomsnake, 23 August 2006 - 12:09 AM.


#3 Hooch

    Young Padawan

  • Members
  • Pip
  • 158 posts
  • Location:Brockville, Ontario

Posted 23 August 2006 - 08:13 AM

View Postvenomsnake, on Aug 23 2006, 12:40 AM, said:

the if statment wont kick in because of the query, if you have where status = 'NULL', it wont select any rows except the rows that are null, so it wont pull any rows that has something in the status field.

if you take out the status = 'null' part, then the if statment will work, and when the field is empty, the else part will go instead.

edit: i just thought of something you can add to it to make it better,

$NULL = mysql_query("SELECT * from users");	
if ($NULL >= 1) {
stuff here
} elseif ($NULL = NULL) {
stuff here
} else {
more stuff
}

if you do it like that, it will show your code in the first part if its a number greater to or equal to 1, else if its null it doesnt something else, then if it has text or a number less then 1, it does something else again.

If there are no NULL entries then the else should kick in, and finish the table saying "All caught up here :blush:"
should it not??
The code you posted would pick up other fields with NULL in them too, correct?

#4 venomsnake

    Jedi In Training

  • Members
  • PipPip
  • 481 posts
  • Gender:Male

Posted 23 August 2006 - 12:16 PM

right, u said ur problem was that the if statment wouldnt kick in when u had rows with numbers in it.

with the code i posted, the if will kick in when it finds rows that have numbers greater to or equal to 1, the elseif will kick in when it finds rows that are null, then the else is just in case there is something in the databse that you dont want, maybe you put a row in wrong, (finds rows that have text, or numbers less then 1)

#5 Hooch

    Young Padawan

  • Members
  • Pip
  • 158 posts
  • Location:Brockville, Ontario

Posted 25 August 2006 - 08:03 PM

I want to try this again.
I really need to know how I am doing this wrong.
Here's the code with numbers (and it is simplified)
1. <table>
2.   <tr>
3.	 <td>Members that need thier status set</td>
4. <? 
5. include("../includes/db.php"); 
6. $NULL = mysql_query("SELECT * from users WHERE status = 'NULL' ");	 
7. if ($NULL >= 1) {
8. while ($r = mysql_fetch_array($NULL)) 
9. { ?>
10.   <tr>
11.	 <td>
12. 	<a href="admin2.php?user=<? echo $r[username]; ?>" target="_self"class="link">
13. 	<? echo $r[firstname]; ?> <? echo $r[lastname]; ?> - (
14. 	<? echo $r[username]; ?>)
15. 	</a>
16. 	</td>	
17.   </tr>
18. <? } ?>
19. </table>
20 .<? } else { ?>
21.   <tr>
22.	 <td>All caught up here :)</td>
23.   </tr>
24. </table>
25. <? } ?>

When status is >= 'NULL' it works from line # 8 till 19.
The else part of the statement is from line # 21 till 25.

The code works perfect if there are members that need thier status set. (status >= NULL)
(the 1st part of the if statement)
But when status is = 'NULL'
The else never shows up. (line # 21 till 25)


venomsnake mentioned....
"the if statment wont kick in because of the query, if you have where status = 'NULL', it wont select any rows except the rows that are null, so it wont pull any rows that has something in the status field.
"
I still think the code is ok. If status is = 'NULL' Then all I want is the table to close out with the
All is caught up here statement.

Here's hoping someone can get through my thick skull.
Thanks for any help.
Hooch

Edited by Hooch, 25 August 2006 - 08:13 PM.


#6 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 25 August 2006 - 09:24 PM

Hmm, I see nothing wrong with anything other than personal preferences, lol.
And just so you know, since it is in single quotes, it should be a string, so make sure the actual 'NULL' is in that row, not a NULL which is an actual term.
I might have the mixed up, I'm not 100% on how SQL interprets single quotes.

Actually now I look at it, you have it all wrong. :P
mysql_query returns strings, not a boolean or an integer.
So, here is what your if statement should look.
if(mysql_num_rows($NULL)){
I think that should work, since you have your code reversed from what I would have. I think that mysql_num_rows should return a TRUE if successful (other than actually setting a number), thus setting the if statement true.
If that doens't work, this is 100% guarenteed to work.
if(mysql_num_rows($NULL) > 0){

;)

#7 Hooch

    Young Padawan

  • Members
  • Pip
  • 158 posts
  • Location:Brockville, Ontario

Posted 26 August 2006 - 11:05 PM

Thank you Demonslay.
I got it working.

Edited by Hooch, 26 August 2006 - 11:06 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users