tags:

views:

114

answers:

5

Hi again, I have a simple html form that submits information with POST function. But when information contains a Cyrillic characters, in table in MySql there becomes азазаза symbols instead of text. The table is on utf-8_general_ci, the site is on UTF-8 encoding. I visualize the result from this table with

$query = "
  SELECT ".$db->nameQuote('ingredients')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC
  ";
$db->setQuery($query);
$ingredients = $db->loadResult();

I cant understand how to tell the form to send chyrillic characters correct. Or where is the problem at all? How to fetch this characters correctly? Or how to send them correctly?

-----------------EDIT-----------------------

I couldn't understand where to put

mysql_query("SET CHARACTER SET utf8"); mysql_query("SET NAMES utf8");

So I'm pasting my code here. First the simple form:

<form action="insert.php" method="post" onsubmit="return checkForm(this)" target="_top">
<table>

<tr>
<td colspan="2">
<ul>
<li> Добавете необходимите за рецептата съставки</li>
<li> Моля попълнете всички полета коректно</li>
<li> Полетата маркирани с (*) са задължителни</li>
</ul>
</td>
</tr>

<tr>
<td>
Количество (порции)*: 
</td>
<td>
<input type="text" name="quantity" />
</td>
</tr>

<tr>
<td>
Съставки*: 
</td>
<td>
<input type="text" name="ingredients" />
</td>
</tr>

<tr>
<td>
Време за приготвяне*:
</td>
<td>
<input type="text" name="timing" /><br />
</td>
</tr>

<tr>
<td></td>
<td>
<input type="submit" value="Напред" class="button validate" />
</td>
</tr>
</table>
</form>

And the fetching syntax inside my insert.php file:

$query = "
  SELECT ".$db->nameQuote('quantity')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC

  ";
$db->setQuery($query);
$quantity = $db->loadResult();

$query = "
  SELECT ".$db->nameQuote('ingredients')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC
  ";
$db->setQuery($query);
$ingredients = $db->loadResult();

$query = "
  SELECT ".$db->nameQuote('timing')."
    FROM ".$db->nameQuote('other')."
    ORDER by id DESC
  ";
$db->setQuery($query);
$timing = $db->loadResult();
A: 

азазаза shows up because you're interpreting a UTF-8 bytestream containing азазаза as ISO-8859-1. Make sure that you set the client encoding to UTF-8, so that the database knows you're sending it UTF-8 bytestreams.

Issue the statement

SET NAMES UTF8
Artefacto
I don't know how to... do this... Please see the EDIT of my question! Thanks in advance
Spoonk
+1  A: 

Try setting collocation?

 mysql_query("SET CHARACTER SET utf8");
 mysql_query("SET NAMES utf8");
Robus
Excuse me but I cant understand where to put that code? Inside my html file that contains form, or when I fetch data from DB?
Spoonk
only one of these is sufficient
Col. Shrapnel
@Spoonk everywhere. just add it to where your connect statement resides. Every time you connect to database, execute a "SET NAMES utf8" query as well, using your usual function to run mysql query.
Col. Shrapnel
I still don't know how to... do this... Please see the EDIT of my question! Thanks in advance
Spoonk
A: 

Double check your database. I've used the utf8_unicode_ci collation for Russian, Estonian (umlauts) etc. and made sure the forms get posted in UTF-8 as well, no problems.

And if you keep some multibyte UTF-8 stuff hardcoded inside the PHP, it's best to make sure the internal PHP encoding is appropriate as well:

mb_internal_encoding("UTF-8");
mb_http_output("UTF-8");
ob_start("mb_output_handler");

And yes, as guys stated here, I'm also using the

@mysql_query("SET NAMES 'utf8'");

in the database class conditionally for MySQL 4.x, but not for MySQL 5. Works fine there, it was a bug reported for 4.1 if I recall correctly.

Ain
SET NAMES should be always used, no matter the version
Col. Shrapnel
A: 

you have to put only one query, SET NAMES one, in your $db class, right after select db statement. using not mysql_query() function but one you're using to execute all other queries.

can you post here the connect statement used in your db class?

Col. Shrapnel
A: 

Your problem may be the browser rather than the database. You should encode the string to utf8 before inserting it into the database to make sure.

Tore A.