Campo vuoto data errore Fatal error:

UGO MILLI

Nuovo Utente
2 Gen 2020
1
0
1
Buonasera a tutti avevo già posto questo quesito ma confondevo il valore null con il campo vuoto e non sapevo poi andare avanti con le domande e facevo un po fatica anche perchè non sapevo trovare il LOG degli errori... abbiate un po di pazienza . Ora che mi sembra chiaro che il problema sia campo VUOTO ...

Quando trova un campo data "datetime" in questo caso 'Manutenzione' VUOTO da l'errore "Fatal error: Call to a member function format() on null i"

GRAZIE !!
PHP:
<?php

$CodiceCliente= $_POST['CodiceCliente'];

$serverName =  "192.168.1.200, 1433";

$connectionOptions = array("Database"=>"xxxxxxxx", "Uid"=>"sa", "PWD"=>"xxxxx");

$conn = sqlsrv_connect($serverName, $connectionOptions);

if (!$conn) {

    die( print_r( sqlsrv_errors(), true));

}

$data = str_pad($CodiceCliente, 8, '0', STR_PAD_LEFT);//print $data;

$tsql = "SELECT * FROM [xxxxxx].[dbo].[Scadenze] where CodiceApice = '".$data."'";

$getProducts = sqlsrv_query($conn, $tsql);


while($row = sqlsrv_fetch_array($getProducts,  SQLSRV_FETCH_ASSOC ))

    {

    $Rifdata   = $row['RifScadenze'];

        echo  '<table width="1000" border="3" class="Stile2">

<td><div align="left">Codice Cliente: ' ,$row['CodiceApice'] , '</div></td>

  <td><div align="left">Ragione_Sociale ' , $row['RagioneSociale'] ,$row['Indirizzo'] ,  '</div></td>

<td><div align="left">Rif ' ,$row['RifScadenze']  ,'  </tr>

           </table>';

            echo("<table border=\"1\">");

    echo("<tr>");

    echo("<td><b>Manutenzione</b></td>");

    echo("<td><b>Matricola</b></td>");

    echo("<td><b>Anno</b></td>");

    echo("<td><b>Kg./lt</b></td>");

    echo("<td><b>4.5</b></td>");

    echo("<td><b>4.6</b></td>");

    echo("<td><b>4.7</b></td>");

        echo("</tr>");

        

         $tsqlMatricole =  "SELECT *  FROM [DATIMILLISQL].[dbo].[Matricole] where Rifscadenze = '".$Rifdata."'";

   $MatricolegetProducts = sqlsrv_query($conn, $tsqlMatricole);//$productCount = 0;

      

   while($rows = sqlsrv_fetch_array($MatricolegetProducts, SQLSRV_FETCH_ASSOC ));


       { echo  '<table width="300" border="1" class="Stile00">


 <td><div align="left">data: ' ,$rows['Manutenzione'] ->format ('d.m.y.'), '</div></td>

<td><div align="center">' ,$rows['Matricola'] , '</div></td>

<td><div align="center">',$rows['Anno'] , '</div></td>

<td><div align="center">',$rows['Capacità'] , '</div></td>

<td><div align="center">' ,$rows['CinquePuntoDue'] , '</div></td>

<td><div align="center">' ,$rows['CinquePuntoTre'] , '</div></td>

<td><div align="center">' ,$rows['CinquePuntoQuattro'] ,'</div></td>

<td><div align="center">' , $rows['RifScadenze']  ,'  </tr>

           </table>';

}

}


$options =  array( "Scrollable" => SQLSRV_FETCH_ASSOC);

sqlsrv_close($conn);

?>

<?php

?>
 
Ultima modifica di un moderatore:
usa error_reporting(-1); che errori ottieni?
Per favore usa il tag codice.
In seguito verifica l'esito diversa da false per ogni query
PHP:
if(($getProducts = sqlsrv_query($conn, $tsql))) {
//tutto il codice se la query funziona
}
PHP:
<?php
$CodiceCliente= $_POST['CodiceCliente'];
$serverName = "192.168.1.200, 1433";
$connectionOptions = array("Database"=>"xxxxxxxx", "Uid"=>"sa", "PWD"=>"xxxxx");
$conn = sqlsrv_connect($serverName, $connectionOptions);
if (!$conn) {
die( print_r( sqlsrv_errors(), true));
}
$data = str_pad($CodiceCliente, 8, '0', STR_PAD_LEFT);//print $data;
$tsql = "SELECT * FROM [xxxxxx].[dbo].[Scadenze] where CodiceApice = '".$data."'";
if(($getProducts = sqlsrv_query($conn, $tsql))) {

while($row = sqlsrv_fetch_array($getProducts, SQLSRV_FETCH_ASSOC ))
{
$Rifdata = $row['RifScadenze'];
echo '<table width="1000" border="3" class="Stile2">
<td><div align="left">Codice Cliente: ' ,$row['CodiceApice'] , '</div></td>
<td><div align="left">Ragione_Sociale ' , $row['RagioneSociale'] ,$row['Indirizzo'] , '</div></td>
<td><div align="left">Rif ' ,$row['RifScadenze'] ,' </tr>
</table>';
echo("<table border=\"1\">");
echo("<tr>");
echo("<td><b>Manutenzione</b></td>");
echo("<td><b>Matricola</b></td>");
echo("<td><b>Anno</b></td>");
echo("<td><b>Kg./lt</b></td>");
echo("<td><b>4.5</b></td>");
echo("<td><b>4.6</b></td>");
echo("<td><b>4.7</b></td>");
echo("</tr>");

$tsqlMatricole = "SELECT * FROM [DATIMILLISQL].[dbo].[Matricole] where Rifscadenze = '".$Rifdata."'";
if(($MatricolegetProducts = sqlsrv_query($conn, $tsqlMatricole))) {//$productCount = 0;

while($rows = sqlsrv_fetch_array($MatricolegetProducts, SQLSRV_FETCH_ASSOC ))
{
if(!isset($rows['Manutenzione']))
$rows['Manutenzione'] = '';

echo '<table width="300" border="1" class="Stile00">

<td><div align="left">data: ' ,$rows['Manutenzione'] ->format ('d.m.y.'), '</div></td>
<td><div align="center">' ,$rows['Matricola'] , '</div></td>
<td><div align="center">',$rows['Anno'] , '</div></td>
<td><div align="center">',$rows['Capacità'] , '</div></td>
<td><div align="center">' ,$rows['CinquePuntoDue'] , '</div></td>
<td><div align="center">' ,$rows['CinquePuntoTre'] , '</div></td>
<td><div align="center">' ,$rows['CinquePuntoQuattro'] ,'</div></td>
<td><div align="center">' , $rows['RifScadenze'] ,' </tr>
</table>';
}
}

$options = array( "Scrollable" => SQLSRV_FETCH_ASSOC);
}
}
sqlsrv_close($conn);
?>
<?php
?>
Intanto prova così poi a limite possiamo utilizzare la sintassi a blocchi di php e eventualmente includiamo l'intera tabella html dopo il secondo while in modo da non scrivere manutenzione per le due liste se non è presente ;)
Comunque format si utilizza per la classe DATETIME php io penso che $rows['manutenzione'] sia semplicemente di tipo varchar anche se avresti potuto utilizzare il tipo DATETIME YYYY-MM-DD HH:mm:ss se è varchar effettua direttamente La Stampa con echo poi magari se prevedi di usarlo per differenti fusi orari si crea una funzione oppure se è effettivamente un un'oggetto DATETIME php puoi includerlo in un'altra variabile e a limite stampi o vuoto oppure la data
PHP:
$manutenzione = '';
if(isset($rows['Manutenzione']))
$manutenzione .= $rows['Manutenzione']->format("Y-m-d");
 
Ultima modifica:
@UGO MILLI
avviso.png
 

Discussioni simili