Solved

query and if statement won't work

Posted on 2003-12-05
5
232 Views
Last Modified: 2006-11-17
Ok. I am looping through the numbers 1 - 31. I have a url variable called month. I have a query and statement that should be like.....if date = '12/5' then link number otherwise, don't link number. For example there is a 12/5 in the database and the url month=12, but right now everything is linking. Here is the code, but something isn't right:

$count = 1;
while ($count <=31)
{
     $sql = "SELECT id from oldies_history where date = '$_REQUEST[month]/$count'";
     $ok = @mysql_query($sql);
          if (!@ok)
               {
                    echo("$count&nbsp;&nbsp;");
               }
          else
              {
                   echo( "<a href='edit_history_detail.php?month=$_REQUEST[month&day=$count'>$count</a></li>&nbsp;&nbsp;" );
              }                              
     $count++;
}
0
Comment
Question by:mappenzellar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 5

Expert Comment

by:arjanh
ID: 9884785
try changing
          if (!@ok)
into
          if (! $ok)
0
 

Author Comment

by:mappenzellar
ID: 9884808
nope caught that, but still no go
0
 
LVL 5

Expert Comment

by:arjanh
ID: 9884824
$ok = @mysql_query($sql);

the '@' is masking all errors I think, so try

$ok = mysql_query($sql);
0
 
LVL 5

Accepted Solution

by:
arjanh earned 50 total points
ID: 9884856
You need to look at the number of records found, not whether or not the query was successful:

$result = @mysql_query($sql);
$num = mysql_num_rows($result);
if ($num == 0)

0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9885048
I believe that when you put arrays directly into strings you have to put braces around them. eg "{$_REQUEST['month']}"

On an aside, you should really put your input through mysql_escape_string(), before sending it to the database.

mysql_query will return a value even if no rows are returned.
If you want to see if a row was returned use mysql_num_rows($result) // where $result is what is returned from mysql_query().

you should be using the if($ok) for error checking, since if it returns false(or 0), you generally have a problem. You shouldn't be using it to check if a row exists, because quite simply that is not how it works.

You also didn't close the square bracket in your altenative option.

I think you are trying to find all the rows with that date, but I'm not quite sure.

I've made a few changes to your code.

$count = 1;
for($count = 1; $count <=31; ++$count) {
     $sql = "SELECT id from oldies_history where date = '{$_REQUEST['month']}/$count'";
     $ok = mysql_query($sql);
     if(!$ok){
        die(sprintf("Query Failed: %s\n", mysql_error());
     }

     $date_exists = mysql_num_rows($ok);
          if (!$date_exists)
               {
                    echo("$count&nbsp;&nbsp;");
               }
          else
              {
                   echo( "<a href='edit_history_detail.php?month={$_REQUEST['month']}&day=$count'>$count</a></li>&nbsp;&nbsp;" );
              }
}
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question