Wrong interpretation of some letters from MYSQL

Wrong interpretation of some letters from MYSQL

Thepaky91Thepaky91 Posts: 4Questions: 2Answers: 0

Hello to everybody,
I have created a table that takes some data from MYSQL. I am using DataTable to customize it. The problem is that some letters aren't read in a correct way. In fact, I found "?" instead of some letters. If I create a table without using DataTable, this problem doesn't appear. How can I fix it?

thanks in advance

Answers

  • colincolin Posts: 15,146Questions: 1Answers: 2,586

    It may be a character encoding issue - some difference between the DB and the web page. It would be worth confirming that they match.

    C

  • Thepaky91Thepaky91 Posts: 4Questions: 2Answers: 0

    How can I do to match the character encoding between DB and webpage? This issue only appear using DataTable. If I create a normal table without this plugin, there are no problems.

  • allanallan Posts: 61,734Questions: 1Answers: 10,111 Site admin

    Try adding:

    $db->sql('SET NAMES utf8');
    

    immediately before your Editor::inst(...) call.

    I'm assuming you are using PHP...

    Allan

  • Thepaky91Thepaky91 Posts: 4Questions: 2Answers: 0

    Sorry, but I don't understand where I have to put it.

    Below the code for database access:

    <?php
    $link = mysqli_connect ("XXXXXXXX", 
    "XXXXX", "XXXXX", "XXXXX"); 
    
    if (mysqli_connect_error()) {
    
        die ("Problemi di connessione con il server. Riprovare più tardi");
    
    };
    ?>
    

    Below the code created for the table:

    <div style="overflow-x:auto;">
    
              <table id="tabellaAlimenti" class="table table-striped">
                  <thead>
                    <tr>
                        <th>Nome</th>
                        <th>Categoria</th>
                        <th>Kcal</th>
                        <th>Carbo (g)</th>
                        <th>Prot (g)</th>
                        <th>Gras (g)</th>
                    </tr>
                  </thead>
    
                  <tbody>
                      <?php
    
                        $query = "SELECT * FROM databaseAlimenti";
    
                        if ($result = mysqli_query($link, $query)) {
    
                            while($row = mysqli_fetch_array($result)) {
    
                                echo "<tr>";
                            echo "<td id='nome'><a href='http://wellness4yourself-com.stackstaging.com/alimento/?nome=" .urlencode($row['Nome']). "'>" . $row['Nome'] . "</a></td>";
                                echo "<td>" . $row['Categoria'] . "</td>";
                                echo "<td>" . $row['kcal'] . "</td>";
                                echo "<td>" . $row['Carboidrati (g)'] . "</td>";
                                echo "<td>" . $row['Proteine (g)'] . "</td>";
                                echo "<td>" . $row['Grassi totali (g)'] . "</td>";
                                echo "</tr>";
    
                            }
                          };
                        ?>
                  </tbody>
    
                </table>
    
            </div>
    
     <script type="text/javascript">
    
              $(document).ready(function() {
    
                  $('#tabellaAlimenti').DataTable( {
    
                      select: true,
                  });
    
    
              });
    
    
          </script>
    

    Thanks in advance

  • allanallan Posts: 61,734Questions: 1Answers: 10,111 Site admin

    I had assumed that you were using the PHP libraries for Editor, which is why my code didn't make any sense.

    I would suggest you use the PHP mysql->set_charset() function since you are using mysqli directly.

    Allan

This discussion has been closed.