lundi 22 mai 2017

if condition in PHP is not working

I have added an if in the below php code, where I need to check if the entry exists or not in a mysql DB.

The variable in the if statement is null and the else is not working, and keeps echoing a blank line. Any idea?

<?php

include ('config/config.php');

$dir = '/home/masteryoda/certs';
$sql = "select * from certificates";

 if (is_dir($dir)){
    if ($dh = opendir($dir)){
         while (($file = readdir($dh)) !== false){
             if ($file != "." && $file != "..") {
                $files = $dir."/".$file;

                $data = openssl_x509_parse(file_get_contents($files));

                $validFrom = date('Y-m-d', $data['validFrom_time_t']);
                $validTo = date('Y-m-d', $data['validTo_time_t']);
                $common = $data['subject']['CN'];
                $issuer = $data['issuer']['CN'];

                $res = $conn->query($sql);

                while ($row = $res->fetch_assoc())
                {
                    if ( $common == $row['CommonName'] && $validFrom == $row['ValidFrom'] && $validTo == $row['ValidTo'] )
                    {

                        echo "Vars:".$common."".$row['CommonName']."".$validFrom."".$row['ValidFrom']."".$validTo."".$row['ValidTo'];
                        echo "Certificate exists";
                        continue;
                    }
                    else
                    {
                        $result = $conn->query("insert into certificates  (CommonName,ValidTo,ValidFrom,Issuer) values ('$common','$validTo','$validFrom','$issuer')");
                    }
                }

            }
        }
        closedir($dh);
    }
 }

Aucun commentaire:

Enregistrer un commentaire