Jump to content

[SOLVED] Getting and showing unicode text from MySQL DB through PHP


Recommended Posts

Hello All,

 

I am trying to obtain some uni-code ( uni-code urdu text ) text from MySQL database and show it on my page generated by using PHP.

 

Original text ( that should be displayed or what I want to display ) may be seen here

http://fahid.freehostia.com/text.html

 

 

And this is the PHP code I am using to get the text from data base

 

 

<?php 

function mysqlcon(){
   $conn = mysql_connect('localhost','root','pass') or die(mysql_error());
   $db = mysql_select_db('wordpress') or die(mysql_error());
   return $conn;
}
function get_wp_news (){
   $conn = mysqlcon();
   $output = '';
   $query = "SELECT * FROM `wp_posts` WHERE `post_category`=0 ORDER BY `post_date` DESC LIMIT 0,1";
   $result = mysql_query($query) or die(mysql_error());
   $rows = mysql_affected_rows();
   if($rows > 0){
      while($db_output = mysql_fetch_array($result)){
         $output = $db_output['post_content'];
      }
   }
   else $output = "No Records Were Found!";
   
   return $output;
}

?>

 

The text is stored in database correctly, it is originally stored into data base using Wordpress, a well know PHP blogging engine. Wordpress shows the text absolutely perfectly.

 

but when I try to obtain the uni-code urdu text from MySQL and show it, it show something like ? ? ? ? ? ? ? ? ? ?? symbols instead of the uni-code urdu text.

 

I hope I am using correct HTML encoding type in the html head, that is

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

 

A working version of my PHP script showing the messed up output is available here http://fahid.freehostia.com/

 

Please help me do it correctly, I can't understand what's going wrong

Thanks God, I have got it done

just a little addition to my php code, goes as under

 

<?php 
function mysqlcon(){
   $conn = mysql_connect('','root','pass') or die(mysql_error());
   $db = mysql_select_db('wordpress') or die(mysql_error());
   $unicode = mysql_query("SET NAMES utf8");
   return $conn;
}
function get_wp_news (){
   $conn = mysqlcon();
   $output = '';
   $query = "SELECT * FROM `wp_posts` WHERE `post_category`=0 ORDER BY `post_date` DESC LIMIT 0,1";
   $result = mysql_query($query) or die(mysql_error());
   $rows = mysql_affected_rows();
   if($rows > 0){
      while($db_output = mysql_fetch_array($result)){
         $output = $db_output['post_content'];
      }
   }
   else $output = "No Records Were Found!";
   
   return $output;
}

?>

notice the line# 5 in the script

$unicode = mysql_query("SET NAMES utf8");

 

Thanks goes to lorenz pressler @ http://dev.mysql.com/doc/refman/5.1/en/charset-unicode.html

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.