0

I have simple PHP test API for my database. It queries MySQL DB and returns JSON. Problem is, some values are in Cyrillic and all I get is a question marks. My PHP code is like this:

<?php
mysql_connect ( "localhost", "root", "" );
mysql_select_db ( "uni_database" );
$q = mysql_query ( "SELECT * FROM `students`" );
while ( $e = mysql_fetch_assoc ( $q ) )
    $output [] = $e;
print  (json_encode( $output ) );
mysql_close ();

?>

Does anyone know how to encode values from DB to UTF8?

miller
  • 1,636
  • 3
  • 26
  • 55

2 Answers2

1

When you create a database make sure it is set to utf8

CREATE DATABASE `myDb` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

Then make the field type as text

Roseann Solano
  • 762
  • 2
  • 8
  • 13
1
mysql_connect ( "localhost", "root", "" );
mysql_select_db ( "uni_database" );
mysql_query("set character_set_connection=utf8");
mysql_query("set names utf8");
...
Vlad
  • 3,626
  • 16
  • 14