-2

i have database:

so result it:

frome this code:

<?php

 
    $dbhost = '?';
    $dbuser = '?';
    $dbpass = '?';


    try {

        $dbh = new PDO("mysql:host=localhost;dbname=?", "?", "?");  
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $result = $dbh->query("SELECT * FROM DATACC");
   
  mysql_set_charset("UTF8");
  header('Content-type: text/html; charset=utf-8');
  

  } catch(PDOException $e) {
        echo $e->getMessage();
    }

 echo "<body>";
 
    echo "<table  class='bordered'>";
   
    foreach($result->fetchAll() as $row){
   echo "<tbody>"; 
    echo "<tr class='alt'>";
     echo "<td width='20px'>" . $row["MaCC"] . "</td>";
     echo "<td>" . $row["HoVaTen_VN"] . "</td>";
     echo "<td>" . $row["NgaySinh"] . "</td>";
     echo "<td>" . $row["NoiSinh"] . "</td>";
     
    
    echo "</tr>";
   echo "</tbody>"; 
    }
    echo "</talbe>";
 
 echo "</body>";
    $dbh = NULL;

?>

can you help me fix this code! (i tried many way i can to fix this error, so still fail). Thanks for this!

Mike 'Pomax' Kamermans
  • 49,297
  • 16
  • 112
  • 153

2 Answers2

0

You are using PDO but at the same time using mysql_set_charset which is not PDO and will not affect your PDO connection.

Instead use a connection string to PDO like:

try {

    $dbh = new PDO("mysql:host=localhost;dbname=?;charset=utf8", "?", "?");
    ...

As a second note:

Open a MySQL terminal and use the following command :

SELECT T.table_name,CCSA.character_set_name 
FROM information_schema.`TABLES` T, information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA 
WHERE CCSA.collation_name = T.table_collation 
     AND T.table_schema = "<your dbname name>";

Make sure the tables are all encoded in UTF-8 as you need them to be.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

There is one and only one proper way to set the character set with PDO and that is as part of the DSN.

$dbh = new PDO("mysql:host=localhost;dbname=?;charset=utf8", "?", "?");