How to not show junk value when pulling a data from a null PHP database

Asked

Viewed 35 times

-2

I’m pulling a value VARCHAR from the bank , I convert it to appear in DATA format , as long as the value is white in my bank, it pulls me a number "31/12/1969" . How can I fix this ?

How I’m turning that value :


*CONEXÃO COM O BANCO + SQL DO SELECT 

<table>
<thead>
 <tr>
   <th>
<center><b>Data</b></center>
    </th>
</thead>
<?php while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) { ?>
<tr>

<td> ?php echo date("d/m/Y",strtotime($row['DT_FATURAMENTO']??'')); ?>
</td>
<?php     }   ?>

 </table>

2 answers

3


Checking whether the value of $row['DT_FATURAMENTO'] is empty before passing it to the function strtotime.

You tried to do this:

strtotime($row['DT_FATURAMENTO'] ?? '')

But:

  • Used the null coalescence operator, ??, which will check whether the first parameter is null, nay emptiness (something empty is non-null);
  • If null, pass as parameter a string '', which is JUST the one you don’t want to pass;

It seems he wrote this piece of code and didn’t even stop to analyze what he had actually done.

You must change the condition, considering $data the value in question:

<?= empty($data) ? 'Indefinida' : date('d/m/Y', strtotime($data)) ?>

1

This is default date for Unix, with time zone setting (00:00:00 UTC on 1 January 1970 +/- Time zone).

To solve the problem, conditionally do not convert to date if varchar is null.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.