Note that there are some explanatory texts on larger screens.

plurals
  1. POStoring and displaying unicode string (हिन्दी) using PHP and MySQL
    text
    copied!<p>I have to store hindi text in a MySQL database, fetch it using a PHP script and display it on a webpage. I did the following:</p> <p>I created a database and set its encoding to UTF-8 and also the collation to <code>utf8_bin</code>. I added a varchar field in the table and set it to accept UTF-8 text in the charset property.</p> <p>Then I set about adding data to it. Here I had to copy data from an <a href="http://www.mypanchang.com/2009/hi/Jammu-Kashmir-India/0/july.html" rel="noreferrer">existing site</a>. The hindi text looks like this: सूर्योदय:05:30 </p> <p>I directly copied this text into my database and used the PHP code <code>echo(utf8_encode($string))</code> to display the data. Upon doing so the browser showed me "??????".</p> <p>When I inserted the UTF equivalent of the text by going to "view source" in the browser, however, सूर्योदय translates into <code>&amp;#2360;&amp;#2370;&amp;#2352;&amp;#2381;&amp;#2351;&amp;#2379;&amp;#2342;&amp;#2351;</code>.</p> <p>If I enter and store <code>&amp;#2360;&amp;#2370;&amp;#2352;&amp;#2381;&amp;#2351;&amp;#2379;&amp;#2342;&amp;#2351;</code> in the database, it converts perfectly.</p> <p>So what I want to know is how I can directly store सूर्योदय into my database and fetch it and display it in my webpage using PHP.</p> <p>Also, can anyone help me understand if there's a script which when I type in सूर्योदय, gives me <code>&amp;#2360;&amp;#2370;&amp;#2352;&amp;#2381;&amp;#2351;&amp;#2379;&amp;#2342;&amp;#2351;</code>?</p> <p><strong>Solution Found</strong></p> <p>I wrote the following sample script which worked for me. Hope it helps someone else too</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;title&gt;Hindi&lt;/title&gt;&lt;/head&gt; &lt;body&gt; &lt;?php include("connection.php"); //simple connection setting $result = mysql_query("SET NAMES utf8"); //the main trick $cmd = "select * from hindi"; $result = mysql_query($cmd); while ($myrow = mysql_fetch_row($result)) { echo ($myrow[0]); } ?&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>The dump for my database storing hindi utf strings is</p> <pre><code>CREATE TABLE `hindi` ( `data` varchar(1000) character set utf8 collate utf8_bin default NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `hindi` VALUES ('सूर्योदय'); </code></pre> <p>Now my question is, how did it work without specifying "META" or header info?</p> <p>Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload