Problem with UTF_8

1

Good morning, my problem is this, Php sends me the following Warning

  

Warning: utf8_decode () expects parameter 1 to be string, object given in.

I have already reviewed my code and I have declared the characterSet in this mode "CharacterSet" => "UTF-8" If I delete it, it does not produce results, the part of the code where the error is thrown is the following

<?php 
            //imprime resultados codificados a utf8 en caso de no haber resultados no hace nada
            if($hay){
                while($result = sqlsrv_fetch_object($results)) {
                    echo "<tr>";
                        foreach ($result as $col) {
                            echo "<td>". utf8_decode($col) ."</td>";
                        }
                    echo "</tr>";
                }
            }
             ?>

All after a query that gives me partial results, now I checked, I did var_dump and I got the gettype of $col and I realized (as in the query includes a field type Datatime) that the date is the one that somehow does not convert and causes this warning, printing almost all data except the date.

Will anyone know what I can do to resolve the warning?

    
asked by Augustoock 25.08.2016 в 18:56
source

2 answers

2

The warning literally says that the utf8_decode function expects to receive a string of characters and you are passing an object to it.

Because of the way your code is written, since the variable $col is changing type according to the result obtained, you have these two alternatives:

apply a type mold

The basic idea is:

$myText = (string)$myVar;

So, it could be:

                        foreach ($result as $col) {
                            $txt = (string)$col;
                            echo "<td>". utf8_decode($txt) ."</td>";
                        }

concatenate

PHP will force the variable to be a string if you concatenate another string, for example:

$txt = $myVar . " "

So, you could leave:

                        foreach ($result as $col) {
                            echo "<td>". utf8_decode($col." ") ."</td>";
                        }

DateTime

After the comment of the OP, I realized that just this will fail with DateTime, I have no way to prove it now, but this should work:

foreach ($result as $col) {
    if ($col instanceof DateTime) {
      $txt = $col->format('Y-m-d H:i:s');
    } else {
      $txt = (string)$col;
    }
    echo "<td>". utf8_decode($txt) ."</td>";
}
    
answered by 25.08.2016 / 19:04
source
1

You are trying to print all the fields of a record in a generic way. If that's not what you're looking for, maybe you should specify them one by one:

<?php
   if($hay)
   {
     while($result = sqlsrv_fetch_object($results)) 
     {
        echo "<tr><td>".
                       $result->campo1."</td><td>". 
                       $result->campofecha->format('Y-m-d H:i:s').
            "</td></tr>";            
     }
   }
?>

This way you should not skip any errors.

Remember to also refer to the manual page . It tells you that the methods of the object returned correspond to the names of the fields and other things necessary to use it.

Good luck ;-)

    
answered by 25.08.2016 в 19:57