?
Solved

PHP code doesn't work. need Guru help

Posted on 2007-10-21
24
Medium Priority
?
283 Views
Last Modified: 2013-12-13
I have this code that was suppose to match the data and work however it doesn't work as the table is empty. How can I go about changing the code so it works.
<?php
            $checksurvey = mysql_query("Select * from tablesurvey_17817 where submitdate =\"N\" or submitdate is NULL or submitdate === Null or submitdate=''") or die("submite date problem");
            $checksurveystatus = mysql_num_rows($checksurvey);
            echo $checksurveystatus;
           
                if ($checksurveystatus) {
           $tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die("Couldn't query the user-database for  Values.");
            $tokenquery = mysql_result($tok);
            if ($tokenquery) {
                  echo "<a href=".$tokenquery."\"\">Take Survey</a>";
            }else{
            echo '<span style="color: red;">Completed !!</span>';
            }
               
           
        }
            else{
           
                echo "Completed By Survey ID";
           
                }
            ?>
Thanks in advance
0
Comment
Question by:syedasimmeesaq
  • 15
  • 6
  • 2
  • +1
24 Comments
 
LVL 21

Expert Comment

by:nizsmo
ID: 20119292
Try and see if this works?


<?php
      // Have you connected to the server using mysql_connect()?

      $checksurvey = mysql_query("Select * from tablesurvey_17817 where submitdate =\"N\" or submitdate is NULL or submitdate === Null or submitdate=''") or die("submite date problem");
      $checksurveystatus = mysql_num_rows($checksurvey);
      echo $checksurveystatus;
      
          if ($checksurveystatus!=0) {
      $tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die("Couldn't query the user-database for  Values.");
      $nrecs = mysql_num_rows($tok);
      $tokenquery = mysql_result($tok,0);
      if ($nrecs!=0) {
            echo "<a href=".$tokenquery."\"\">Take Survey</a>";
      }else{
      echo '<span style="color: red;">Completed !!</span>';
      }
        
      
      }
      else{
      
          echo "Completed By Survey ID";
      
          }
?>
0
 
LVL 10

Expert Comment

by:NeoTeq
ID: 20119305
In the first line of opening the query, I suspect you do not get any results. After that, the second line will either prompt an error or give you no count.

$checksurvey = mysql_query("Select * from tablesurvey_17817 where submitdate =\"N\" or submitdate is NULL or submitdate === Null or submitdate=''") or die("submite date problem");
$checksurveystatus = mysql_num_rows($checksurvey);

Remove the === null check, since that won't work in MySQL:

 $checksurvey = mysql_query("Select * from tablesurvey_17817 where submitdate =\"N\" or submitdate is NULL or submitdate=''") or die("submite date problem");
$checksurveystatus = mysql_num_rows($checksurvey);
0
 
LVL 10

Expert Comment

by:NeoTeq
ID: 20119315
Also, keep in mind that just because you get a resultset, doesn't mean there are records. Use a check like nizsmo or lie this:

  $tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die("Couldn't query the user-database for  Values.");

      if ($tokenquery  = mysql_fetch_row($tok)) {
            echo "<a href=".$tokenquery."\"\">Take Survey</a>";
      }else{
      echo '<span style="color: red;">Completed !!</span>';
      }
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119382
God know why but I am still getting this error
      
      
submite date problem
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119383
could it be because table is empty?
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119392
ok..development!!!!!!!!!!!!!!!!!!!!!!!

now its not spitting out an error of submite Date but its not showing Take aurvey or completed. It should be showing Take Survey.
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119464
Any idea?
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20119519
$check_table = mysql_query("SELECT * FROM tablesurvey_17817");

if(mysql_num_rows($check_table)) {
      
      $checksurvey = mysql_query("Select * from tablesurvey_17817 where submitdate =\"N\" or submitdate is NULL or submitdate=''") or die(mysql_error());
      
      $checksurveystatus = mysql_num_rows($checksurvey);
      
      echo $checksurveystatus;
      
      if ($checksurveystatus ) {
            
            $tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die(mysql_error());
      
            $tokenquery = mysql_result($tok);
      
            if ($tokenquery) {
            
                  echo "<a href=".$tokenquery."\"\">Take Survey</a>";
            
            }else{
            
                  echo '<span style="color: red;">Completed !!</span>';
            
            }
      
      }else{
      
      echo "Completed By Survey ID";
      
      }


}else{


      echo "No Records in table.";


}
            ?>
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119533
steelseth12: thank you for your response. Now I get No records in table. I am sure there are records in the table.
Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119540
I seen at one place that some one called token field using trim. Could that be a problem?
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20119542
No records in table is the message i put to echo if the table is empty.

If you got that then there are no records in the table.

SELECT * FROM tablesurvey_17817

The above query selects all records from table tablesurvey_17817 ... there are not WHERE clauses ... if there are any records it will return them.

If its empty then it will give you the message
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119543
Also there are two tables used in this code. Which table exactly does it say is empty.
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119552
ok then how can I make the code that if table tablesurvey_17817 is emoty then run this part
 $tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die(mysql_error());
     
            $tokenquery = mysql_result($tok);
     
            if ($tokenquery) {
           
                  echo "<a href=".$tokenquery."\"\">Take Survey</a>";
           
            }else{
           
                  echo '<span style="color: red;">Completed !!</span>';
           
            }

Thanks again for your help
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20119553
From what i can see in the code ... the only table is table_17817 ....

The trim function simply removes any spaces from the beginning and end of a string
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20119558
If table_17817 is empty then

 $tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die(mysql_error());

will always return FALSE since there are no records in the table.
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119560
or better yet if we can code it someway that
if tablesurvey_17817 is emoty or have completed field set equal to N
then run the above code.
$tokenquery = mysql_result($tok);
     
            if ($tokenquery) {
           
                  echo "<a href=".$tokenquery."\"\">Take Survey</a>";
           
            }else{
           
                  echo '<span style="color: red;">Completed !!</span>';
           
            }

Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119569
No there are two different tables (sorry for confusion) tablesurvey_17817 and table_17817. They are 2 different tables

So if table_survey_17817 is empty or has the completed field is set to N or Null,  the code for table_17817 run.

Thank you
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119579
I believe your code above will work fine if we can only find out a way that run this code
$tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die(mysql_error());
     
            $tokenquery = mysql_result($tok);
     
            if ($tokenquery) {
           
                  echo "<a href=".$tokenquery."\"\">Take Survey</a>";
           
            }else{
           
                  echo '<span style="color: red;">Completed !!</span>';
           
            }

 if the table_survey_17817 is emoty or if table_survey_17817's completed field is set to 'N" or empty.

Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119582
table_survey_17817 is actually emoty right now but it will not be later on. However table_17817 has records in it and is not empty.

Thank you
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 2000 total points
ID: 20119589
Sorry didnt notice it had 2 tables

try this

$check_table = mysql_query("SELECT * FROM tablesurvey_17817");

$full_result = mysql_num_rows($check_table);


      
$checksurvey = mysql_query("Select * from tablesurvey_17817 where submitdate =\"N\" or submitdate is NULL or submitdate=''") or die(mysql_error());

$checksurveystatus = mysql_num_rows($checksurvey);

echo $checksurveystatus;

if ($checksurveystatus || !$full_result) {
      
      $tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die(mysql_error());

      $tokenquery = mysql_result($tok);

      if ($tokenquery) {
      
            echo "<a href=".$tokenquery."\"\">Take Survey</a>";
      
      }else{
      
            echo '<span style="color: red;">Completed !!</span>';
      
      }

}else{

echo "Completed By Survey ID";

}
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119599
ok little development but results are not right
it says completed !! even though the field completed in table_17817 is set to N
it should be saying Take Survey..shouldn't it?

Thanks
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119605
ok here is another development

When I changed it to this

$tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed =\"N\"") or die(mysql_error());

      $tokenquery = mysql_result($tok,0);

it said Take Survey

now I don't know why this ($tok,0); would work and ($tok) would not. And is it a correct result?

Thanks
0
 
LVL 20

Expert Comment

by:steelseth12
ID: 20119620
mysql_result takes 2 arguments the results and the row ...

You must have display errors set to Off .

Although that will work a  more correct way to check would be

$tok = mysql_query("SELECT linkadd FROM table_17817 where token ='".$_SESSION['pass']."' AND completed ='N'") or die(mysql_error());

      

      if (mysql_num_rows($tok)) {
      
            echo "<a href=".$tokenquery."\"\">Take Survey</a>";
      
      }else{
      
            echo '<span style="color: red;">Completed !!</span>';
      
      }
0
 
LVL 9

Author Comment

by:syedasimmeesaq
ID: 20119642
ok thank you for your help. Your method worked for me.
Cheers
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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.
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

839 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