How to display in php special characters from mysql

Discuss coding issues, and scripts related to PHP and MySQL.
User avatar
JanMolendijk
Posts: 282
Location: Holland Rotterdam

How to display in php special characters from mysql

I prefer not to bother you too often but because of my bad English, two accounts have already been blocked on stack overflow.
And I think you are mutch better

I have this script but I get strange results with special characters like € or Ë
How can I make those display correctly?

Code: Select all

        <?php include_once 'Page/data.php'; ?>
        
         <?php 
                    foreach($result as $row) { 
                        echo '
  <span class="w3-right w3-opacity">
'. $row['itime']. '
</span>
        <p>'. $row['ibody']. '</p>
      </div> <br><br><br>
';
    }
?>
Page/data.php

Code: Select all

<?php 
    //include configuration file
    require 'configuration.php';

    $start = 0;  $per_page = 4;
    $page_counter = 0;
    $next = $page_counter + 1;
    $previous = $page_counter - 1;
    
    if(isset($_GET['start'])){
     $start = $_GET['start'];
     $page_counter =  $_GET['start'];
     $start = $start *  $per_page;
     $next = $page_counter + 1;
     $previous = $page_counter - 1;
    }
    // query to get messages from messages table
    $q = "SELECT *   FROM politicsnucleus_item WHERE iblog=1 ORDER BY inumber DESC   LIMIT $start, $per_page ";
    $query = $db->prepare($q);
    $query->execute();

    if($query->rowCount() > 0){
        $result = $query->fetchAll(PDO::FETCH_ASSOC);
    }
    // count total number of rows in students table
    $count_query = "SELECT * FROM politicsnucleus_item   ";
    $query = $db->prepare($count_query);
    $query->execute();
    $count = $query->rowCount();
    // calculate the pagination number by dividing total number of rows with per page.
    $paginations = ceil($count / $per_page);
?>

Admin Posts: 805
Try this:
- At the beginning of your php script, for example at the beginning of the configuration.php file add the header function for utf-8 characters:

Code: Select all

header('Content-type: text/html; charset=utf-8');
- If you use PDO for connetion to mysql, after the connection is defined add this row of code to set transfer data with encoding utf-8 between mysql and php:

Code: Select all

$db->exec('SET character_set_client="utf8",character_set_connection="utf8",character_set_results="utf8";');
- The html page must have the meta tag for utf-8 in the <head> zone (the following is for html5):

Code: Select all

<meta charset="utf-8" />
Also, you can search on the net: display in php special characters utf-8 from mysql.

I suppose that you were not banned for bad english, but for the links you have in the code you post (links to website with indecent and unacceptable content).
- You can try to post your questions in other big forums, for example: sitepoint.com

Similar Topics