Does UTF encode or decode an array?

0

I am making a form where I have to show a survey that is already hosted in the BD. I have the problem that it takes strange characters ...

  

"Mr./Sra.CASER MIRO ROTUE The technician gave you information on the operation of the installed equipment?"

How can I do it? I tried UTF before the row and nothing ... Here I leave the code I have.

$sql3="SELECT * FROM tbl_consumo_final INNER JOIN tbl_visitas ON tbl_consumo_final.PRIMARIA_VISITAS=tbl_visitas.PRIMARIA INNER JOIN tbl_encuesta ON tbl_consumo_final.NUM_CONSUMO=tbl_encuesta.NUM_CONSUMO WHERE tbl_visitas.OT=".$ot."";

            echo "<table id='tablatres'>

                        <thead>
                             <tr>
                                <td colspan='' rowspan='' headers=''> <h3>Preguntas</h3> </td>
                                <td colspan='' rowspan='' headers=''> <h3>Respuestas</h3> </td>
                            </tr>
                         </thead>";

        if($resultado3=$mysqli->query($sql3)) {

            while ($row = $resultado3->fetch_assoc()) {

                echo "<tbody>
                            <tr>
                                <td>$row[PREG_ENCUESTA]</td>
                                <td colspan='' rowspan='' headers=''> $row[RESP_ENCUESTA]</td>
                            </tr>";


            }

             echo "  </tbody>
               </table>";

               $resultado3->free();
        }
    
asked by Rene Limon 29.03.2017 в 18:09
source

3 answers

2

Before going directly to the Database, I would look for the cause of the error in a priority order, in which the last one would be precisely the data.

Possible causes of the problem:

  • That the HTML document does not have the charset set to utf- 8 , which is done with a meta tag in the header:

    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

  • That PHP does not have the charset established to utf-8. If it is a web page in shared hosting, sometimes the chartset is dislocated, when we change the PHP version or when there are updates in the server, or when we delete the .htaccess file (I say it for experience).

  • Now yes! If the cause is neither 1 nor 2 then the problem would be in the database. But do not attack the data so fast! First verify that you are giving a charset correct when you make your connection . If so, then yes, check the charset of the database and that specific column. If it is not utf-8, you can change the charset of the entire database, of the entire table, or of a specific column. Of course, before making changes of this type it is convenient to make a backup of the database ... you never know, something can go wrong. To change the charset, see the MySQL indications , well, I guess it's the system you use.

    Although if it worked before and now it does not, it is possible that the cause is not the database, but the indicated in 1 or 2.

  • answered by 29.03.2017 в 19:45
    0

    Of course you should work in your database with UTF8 to avoid these problems, likewise when you make an insert indicate that the action is also UTF8. To solve the current problem just add:

    $mysqli->set_charset("utf8");
    $sql = "SELECT * FROM tbl_consumo_final";
    

    With this, at the moment your query is executed, you should treat your charset as UTF8.

    For more information, add a link to the official PHP

    documentation.     
    answered by 29.03.2017 в 19:26
    0

    As best suits you, make any of the following suggestions:

    Convert to query

    In your $sql3="SELECT * FROM tbl_consumo_final INNER JOIN tbl_visitas ON ..." place the fields and use conversion with CONVERT(column2 USING utf8) ; so I see the data is $row[RESP_ENCUESTA] so it would be $sql3="SELECT CONVERT(RESP_ENCUESTAUSING utf8) RESP_ENCUESTA, [los demas campos] FROM tbl_consumo_final INNER JOIN tbl_visitas ON ..."

    Convert to get the data

    In the part of your code:

    echo "<tr>
             <td>".utf8_encode($row["PREG_ENCUESTA"])."</td>
             <td colspan='' rowspan='' headers=''>".utf8_encode($row[RESP_ENCUESTA])."</td>
          </tr>";
    

    The TBODY that you place inside the while does not go; it must be outside the while before the iteration; the while should only contain the formation of TR and TD as appropriate.

    Convert the data in each table

    Surely it will not be the only data where the symbols will happen to you; previous backup of the database (and information) do

    UPDATE [TABLA] SET [CAMPO1]=CONVERT(CAMPO1 USING utf8), [CAMPO2]=CONVERT(CAMPO2 USING utf8)...[CAMPOn]=CONVERT(CAMPOn USING utf8) 
    

    for each table; this implies that the information in the following will be saved in UTF8 format (by the systems that feed this data) and no conversion will be necessary (as indicated above) and the data is obtained to be displayed directly (ie you would not move to your code nothing, just update the data in the tables).

        
    answered by 29.03.2017 в 18:38