0

I am populating rows inside a table from Mysql, I have an echo <td> inside a table, what I've been trying to accomplish is inside the while mysql_fetch_array, to echo " Absent" IF the query didn't return any records for that specific html table row, I tried using the mysql_num_rows==0, but it doesn't work as there are rows that DO exist in the query and thus using this approach will not work.

$sqlsun = "SELECT * FROM cscc_attendance WHERE cscc_attendance.empId= '$row[empId]' AND cscc_attendance.login_datetime>'".$this_start1->format('Y-m-d 00:00:00')."' AND cscc_attendance.login_datetime<'".$this_start1->format('Y-m-d 23:59:59')."' GROUP BY cscc_attendance.empId";

    $sqlsunexe = mysql_query($sqlsun);

     echo "<td style=\"background-color:#e6e6e6\"><b>";

      while($rowdate1 = mysql_fetch_array($sqlsunexe))
    {   
     if (empty($rowdate1["login_datetime"])) 
        {
            //IF NO MYSQL RESULT, ECHO TO ROW TD:
            echo "ABSENT";
        }
        else
        {
            //THERE IS A QUERY RESULT, FILL THE TD IN TABLE:
            $newhour = new DateTime($rowdate1["ready_time"]);
            $newhourmodstr = $newhour->modify('-1 hour');
            echo $newhourmodstr->format('H:i:s');
        }
    }
   echo "</b></td>";"
1
  • Could you please provide the sql for the query? Is the column "login_datetime" ever truly empty with your select statement? Commented Jan 6, 2016 at 19:42

1 Answer 1

1

This is how your code should be using mysqli:

<?php
$con = mysqli_connect("localhost", "root", "", "database");
// Evaluate the connection
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
} else {
    $q = "SELECT * FROM `tablename` WHERE `fieldname` = 'value'";
    $sqlsunexe = mysqli_query($con, $q);

    echo "<td style=\"background-color:#e6e6e6\"><b>";
    // check if return data
    if ($sqlsunexe->num_rows > 0){
        while($rowdate1 = mysqli_fetch_array($sqlsunexe)){
            //THERE IS A QUERY RESULT, FILL THE TD IN TABLE:
            $newhour = new DateTime($rowdate1["ready_time"]);
            $newhourmodstr = $newhour->modify('-1 hour');
            echo $newhourmodstr->format('H:i:s');
        }
    } else {
        //IF NO MYSQL RESULT, ECHO TO ROW TD:
        echo "ABSENT";
    }

   echo "</b></td>";
}

?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.