Agnostikguy
Agnostikguy

Reputation: 5

echo value inside while mysql_fetch_array if $result doesn't exist

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>";"

Upvotes: 0

Views: 954

Answers (1)

Jaya Vishwakarma
Jaya Vishwakarma

Reputation: 1332

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>";
}

?>

Upvotes: 1

Related Questions