MySQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsDatabasesMySQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here
  #1  
Old June 26th, 2003, 01:40 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Some MySQL and PHP code not working as it should

I am trying to get my webpage to list titles of articles in a select box on an HTML form. THe only ones that should be showing are those of three types. Yet it still shows every article. HERE is the Code

PHP Code:
<?php
                  $submit
=$submit;
      if(!
$submit)
       {       
        
$newsquery=mysql_query("SELECT rownum FROM news WHERE type=1 || type=2 || type=3 ORDER BY rownum DESC")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
        
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
        
$numrow=$news['rownum'];
        
?>
        <center>
        <form method="get" action="ednews.php">
        <select name="article">
        <option selected value="0">Choose an article to edit</option>
        <?php
        
while($numrow >=1)
         {          
             
$newsquery=mysql_query("SELECT * FROM news WHERE rownum='$numrow' LIMIT 1")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
             
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
          if(
$news['type'] == 1||2||3)
           {
               require(
'class.newstype.php');
                 
$posted=date("m/d/Y"strtotime($news['date']));       
                 
printf("<option value='%s'>%s-%s-%s</option>",$news['id'],$news['title'],$newstype,$posted);
            
$numrow--; 
           }
          else
           {
            
$numrow--;
           }
            }
        
?>
        </select><br>
        <input type="submit" name="submit" value="Edit the above selected article">
        </form>
        </center>
        <?php
       
}


As far as I can tell it should work. I have echoed out the type on the initial query and it kept grabbing the correct article first.But when I click on the arrow to show the choices it shows me all the articles from the first row it grabs to the last article in the DB no matter what type of article it is
__________________
CHornJr
"One day I'll know what I am doing"
My Blog
Suanhacky Lodge #49
Rebel Squadrons

Reply With Quote
  #2  
Old June 26th, 2003, 02:17 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Try changing this line:
PHP Code:
 SELECT rownum FROM news WHERE type=|| type=|| type=

to this:
PHP Code:
 SELECT rownum FROM news WHERE type In(1,2,3
__________________
__________________________________________________ _
Wil Moore III, MCP | Integrations Specialist | Senior Consultant
Are You Listed...? | DigitallySmooth Inc.

Reply With Quote
  #3  
Old June 26th, 2003, 11:12 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
That did not solve the problem but it didn't make things worse as far as I can tell either.

My best guess is that my error is somewhere in the if statement.

PHP Code:
while($numrow >=1)
{          
$newsquery=mysql_query("SELECT * FROM news WHERE rownum='$numrow' LIMIT 1")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
if(
$news['type'] == 1||2||3)
{
require(
'class.newstype.php');
$posted=date("m/d/Y"strtotime($news['date']));       
printf("<option value='%s'>%s-%s-%s</option>",$news['id'],$news['title'],$newstype,$posted);
$numrow--; 
}
else
{
$numrow--;
}


Last edited by CHornJr : June 26th, 2003 at 11:19 PM.

Reply With Quote
  #4  
Old June 27th, 2003, 12:36 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Now I am certain it is teh If statement

I set up a quick page for debugging this code using similar code as to my original except there is no form. Here is the first batch of code I used
PHP Code:
<?php
require('class.dbcon.php');
$newsquery=mysql_query("SELECT rownum FROM news WHERE type In(1,2,3) ORDER BY rownum DESC")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
$numrow=$news['rownum'];
while(
$numrow >=1)
{          
$newsquery=mysql_query("SELECT * FROM news WHERE rownum='$numrow' LIMIT 1")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
$typenews=$news['type'];
if(
$typenews == || || 3)
{       
printf("news type: %s, table row: %s<br>",$news['type'],$news['rownum']);
$numrow--; 
}
else
{
$numrow--;
}
}
?>

This code produced the same results as when I had the form. It got the first row correct but then showed all the subsequent rows no matter what type it should be showing

Here is the second set of code I used
PHP Code:
<?php
require('class.dbcon.php');
$newsquery=mysql_query("SELECT rownum FROM news WHERE type In(1,2,3) ORDER BY rownum DESC")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
$numrow=$news['rownum'];
while(
$numrow >=1)
{          
$newsquery=mysql_query("SELECT * FROM news WHERE rownum='$numrow' LIMIT 1")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
$typenews=$news['type'];
if(
$typenews != || || 3)
{
$numrow--; 
}
else
{       
printf("news type: %s, table row: %s<br>",$news['type'],$news['rownum']);
$numrow--;
}
}
?>

This gave me a blank page. To me this means it is not evaluating properly the statement in the parenthesis '()' of the if statement . I unfortunetly have no lcue as to why

On the Database column type is a tinyint with a length of 1 'tinyint(1)' and is unsigned and indexed.

Reply With Quote
  #5  
Old June 27th, 2003, 01:21 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 233 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 50 m 19 sec
Reputation Power: 6
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Yahoo! I got it working properly

I started playing around with it some more and started looking through the PHP manual to see if I can find why it wasn't working.

I read the while statement info and in the comments someone mentioned while loops with brackets didn't work but it did with the alternative syntax. It gave me the same results. Then I tried a do...while loop. Same results as witht he while loop. I then tried using a switch statement. At first it gave me a problem the seconde time around it almost worked corectly. in the DB right now there were 6 articles that matched the type and it gave the first 3 that matched the type twice (ie. article 1, article 1, article 2, article 2) then just reading around I found out (which I didn't know before cause I didn't have need to use it) that php supported break statements and I got it to work.

here is the final code I ended up with

PHP Code:
 $newsquery=mysql_query("SELECT rownum FROM news WHERE type In(1,2,3) ORDER BY rownum DESC")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());

$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
$numrow=$news['rownum'];
?>
<center>
<form method="get" action="ednews.php">
<select name="article">
<option selected value="0">Choose an article to edit</option>
<?php
while($numrow >=1)
{          
$newsquery=mysql_query("SELECT * FROM news WHERE rownum='$numrow' LIMIT 1")or die("Could not query the news table. Please inform the <a href='mailto:webmaster@suanhacky.org>webmaster</a> immediately. ".mysql_error());
$news=mysql_fetch_array($newsquery)or die("Could not create news array. Please inform the <a href='mailto:webmaster@suanhacky.org'>webmaster</a> immediately. ".mysql_error());
switch(
$news['type'])
{
case 
1:
require(
'class.newstype.php');
$posted=date("m/d/Y"strtotime($news['date']));       
printf("<option value='%s'>%s-%s-%s</option>",$news['id'],$news['title'],$newstype,$posted);
$numrow--;
break 
1;
case 
2:
require(
'class.newstype.php');
$posted=date("m/d/Y"strtotime($news['date']));       
printf("<option value='%s'>%s-%s-%s</option>",$news['id'],$news['title'],$newstype,$posted);
$numrow--;
break 
1;
case 
3:
require(
'class.newstype.php');
$posted=date("m/d/Y"strtotime($news['date']));       
printf("<option value='%s'>%s-%s-%s</option>",$news['id'],$news['title'],$newstype,$posted);
$numrow--;
break 
1;
default:
$numrow--;
break 
1;
}
}
?>
</select><br>
<input type="submit" name="submit" value="Edit the above selected article">
</form>
</center> 


I posted this code so if anyone has this problem in the future with nesting if statements in while loops an answer is already here for them to refer to.

I do have a question however. Have any of you nested if statements in while loops and got it to work correctly (with either normal or alternative syntax) just out of curiosity

Reply With Quote
  #6  
Old June 27th, 2003, 04:26 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
Yes, actually, what you were doing should have worked. I've written code similar and had it work.

I'll see if I can find some.

In the meantime, I think your original problem could have been solved making absolute sure you were evaluating equality between the same types.

The easiest way to accomplish this for me is to just do a var_dump on any questionable type.

In your case:
PHP Code:
 var_dump($typenews

would be a good start.

If it comes back as anything other than an integer you have found your original problem.

Reply With Quote
  #7  
Old June 27th, 2003, 04:31 AM
laidbak laidbak is offline
you know how we do
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jun 2002
Location: In Tha IE -- San Bernardino COUNTY
Posts: 788 laidbak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 4 m 2 sec
Reputation Power: 7
Send a message via ICQ to laidbak Send a message via AIM to laidbak Send a message via MSN to laidbak Send a message via Yahoo to laidbak
In looking at your code even further, I can see that your new code is better. A case statement does better delimit your statements and allows for better flow.

Simply put, it is just better code IMHO.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsDatabasesMySQL Development > Some MySQL and PHP code not working as it should


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |