PHP code doesn't work. need Guru help

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
LVL 9
syedasimmeesaqAsked:
Who is Participating?
 
steelseth12Connect With a Mentor Commented:
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
 
nizsmoDeveloperCommented:
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
 
NeoTeqCommented:
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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
NeoTeqCommented:
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
 
syedasimmeesaqAuthor Commented:
God know why but I am still getting this error
      
      
submite date problem
Thanks
0
 
syedasimmeesaqAuthor Commented:
could it be because table is empty?
0
 
syedasimmeesaqAuthor Commented:
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
 
syedasimmeesaqAuthor Commented:
Any idea?
0
 
steelseth12Commented:
$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
 
syedasimmeesaqAuthor Commented:
steelseth12: thank you for your response. Now I get No records in table. I am sure there are records in the table.
Thanks
0
 
syedasimmeesaqAuthor Commented:
I seen at one place that some one called token field using trim. Could that be a problem?
0
 
steelseth12Commented:
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
 
syedasimmeesaqAuthor Commented:
Also there are two tables used in this code. Which table exactly does it say is empty.
0
 
syedasimmeesaqAuthor Commented:
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
 
steelseth12Commented:
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
 
steelseth12Commented:
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
 
syedasimmeesaqAuthor Commented:
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
 
syedasimmeesaqAuthor Commented:
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
 
syedasimmeesaqAuthor Commented:
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
 
syedasimmeesaqAuthor Commented:
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
 
syedasimmeesaqAuthor Commented:
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
 
syedasimmeesaqAuthor Commented:
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
 
steelseth12Commented:
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
 
syedasimmeesaqAuthor Commented:
ok thank you for your help. Your method worked for me.
Cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.