Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
731 views
in Technique[技术] by (71.8m points)

setting utf8 with mysql through php

I have the following very simple code, which retrieves utf8 formatetd data, such as containing umlauts from a mysql database, which may or may not be set as utf8. If I use either of the commented out approaches to ensure that utf8 data is returned, the data will NOT be returned as utf8, however if I leave them off, the data will be displayed. Why would forcing utf8 negate displaying data as utf8?

<?php
  $con = mysqli_connect("localhost", "x", "", "x");
  //$con->query("SET NAMES 'utf8'");
  //$con-set_charset('utf8');
  $recordsQuery = "SELECT ARTICLE_NAME FROM AUCTIONS1";

  if ($getRecords = $con->prepare($recordsQuery)) {
        $getRecords->execute();
        $getRecords->bind_result($ARTICLE_NAME);

        while ($getRecords->fetch()) {
        echo "<p>$ARTICLE_NAME";
             }
    } else {
        print_r($con->error);
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Perhaps the error lies in the charset you're serving the page with. If you're getting UTF-8 content out of the database and serving it with the default HTML charset of Windows-1252 then it's going to look garbled. Make sure you have the equivalent of this:

header( 'Content-Type: text/html; charset=utf-8' );

in your PHP code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...