0

Again problem with script when i use cyrillic letters. I have simple php script witch gathers data from form, sets that in mysql db, and displays that data on the web page. The script is the simpliest as it can be. Im new in php programming so i watch many tuts, so to set story short, its simple script. It works ok when i use latin letters. BUT. When i use the same script (which obviously works) for inputs in cyrillic letters it shows error on "mysql_fetch_error"!? I red many coments about similar problem on internet, and theres no simple answer. Collation is ok, i tried with utf8-unicode, utf8-general and utf8-bin, again nothing.

 <?php
 if(isset($_POST['submit'])){
$con=mysql_connect('x','x','x');
    if(!$con){
    die("Cant connect:".mysql_error());
 }
 mysql_query('SET NAMES "UTF8"');

 mysql_select_db("x",$con);
 $ins = sprintf('INSERT INTO x  (`Име_и_презиме`,`Датум_рођења`,`Занимање`,`Град`,`Земља`,`Имејл`,`Скајп`) VALUES 
("%s", "%s", "%s", "%s", "%s", "%s", "%s")', 
mysql_real_escape_string($_POST['ime']),
mysql_real_escape_string($_POST['datum']),
mysql_real_escape_string($_POST['zanimanje']),
mysql_real_escape_string($_POST['grad']),
mysql_real_escape_string($_POST['zemlja']),
mysql_real_escape_string($_POST['email']),
mysql_real_escape_string($_POST['skype'])
);
mysql_query($ins,$con);
mysql_close($con);
}
?>

<?php
$con=mysql_connect('x','x','x');
if (!$con){
die("Cant connect:" . mysql_error());
}
mysql_query('SET NAMES "UTF8"');

mysql_select_db ("x",$con);
$sql="SELECT * FROM x"; 
$myData=mysql_query($sql,$con);
echo "<table border=1>
<tr>
<td>Име и презиме</td>
<td>Датум рођења</td>
<td>Занимање</td>
<td>Град</td>
<td>Земља</td>
<td>Email</td>
<td>Skype</td>
 </tr>";
 while($record=mysql_fetch_assoc($myData)){
 echo "<tr>";
 echo "<td>" . htmlspecialchars($record['Име_и_презиме']) . "</td>";
 echo "<td>" . htmlspecialchars($record['Датум_рођења']) . "</td>";
 echo "<td>" . htmlspecialchars($record['Занимање']) . "</td>";
 echo "<td>" . htmlspecialchars($record['Град']) . "</td>";
 echo "<td>" . htmlspecialchars($record['Земља']) . "</td>";
 echo "<td>" . htmlspecialchars($record['Имејл']) . "</td>";
 echo "<td>" . htmlspecialchars($record['Скајп']) . "</td>";

 echo "</tr>";}
 echo "</table>";   

 mysql_close($con);  
 ?>

ERROR: Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/xyz/public_html/form.php on line 46

Kreator
  • 15
  • 2
  • 7
  • 1
    what script? show us some code –  Jan 24 '13 at 22:40
  • its very very basic script. with no validation. if you again want to see ill show it – Kreator Jan 24 '13 at 22:45
  • 1
    [**Please, don't use `mysql_*` functions in new code**](http://bit.ly/phpmsql). They are no longer maintained [and are officially deprecated](http://j.mp/XqV7Lp). See the [**red box**](http://j.mp/Te9zIL)? Learn about [*prepared statements*](http://j.mp/T9hLWi) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://j.mp/QEx8IB) will help you decide which. – Kermit Jan 24 '13 at 22:58
  • the web page needs to be , the db collation also, and the db connection - http://stackoverflow.com/questions/279170/utf-8-all-the-way-through –  Jan 24 '13 at 22:59
  • @Kreator Update your original post with code formatting!! thanks – Phorce Jan 24 '13 at 23:03

1 Answers1

0

Make shure your php file in UTF-8, try:

<?php

if(isset($_POST['submit'])){
    $con = mysql_connect('x','x','x');
    if(!$con){
        die("Cant connect:".mysql_error());
    }
    mysql_query('SET NAMES "UTF8"');

    mysql_select_db("x",$con);
    $ins = sprintf('INSERT INTO ДБ (`Име_и_презиме`,`Датум_рођења`,`Занимање`,`Град`,`Земља`,`Имејл`,`Скајп`) VALUES ("%s", "%s", "%s", "%s", "%s", "%s", "%s")', mysql_real_escape_string($_POST[ime]),mysql_real_escape_string($_POST[datum]),mysql_real_escape_string($_POST[zanimanje]),mysql_real_escape_string($_POST[grad]),mysql_real_escape_string($_POST[zemlja‌]),mysql_real_escape_string($_POST[email]),mysql_real_escape_string($_POST[skype])
);
    mysql_query($ins,$con);
    mysql_close($con);
}
?>

And please, use mysqli extension instead...

clover
  • 4,910
  • 1
  • 18
  • 26
  • Sory ppl for slopyness. Today i started using stackoverflow so im a littlebit slopy. Now you can see code fully. – Kreator Jan 24 '13 at 23:11
  • The basic idea: `mysql_query('SET NAMES "UTF8"');` after connection. Use `mysql_fetch_assoc()` instead of `mysql_fetch_array()`, apostrophes to quote identifiers (table names) and UTF-8 charset for PHP file. – clover Jan 24 '13 at 23:17
  • what should i enter in the place of "%s"? – Kreator Jan 24 '13 at 23:20
  • `mysql_real_escape_string($_POST['varname']);` – clover Jan 24 '13 at 23:20
  • and what about second script? whats your opinion about that? – Kreator Jan 24 '13 at 23:24
  • all the same `mysql_query('SET NAMES "UTF8"');` please read hire http://stackoverflow.com/questions/2159434/set-names-utf8-in-mysql – clover Jan 24 '13 at 23:26
  • ok ill read linked theme. does it mean that structure of second script is ok? That i just need to add SET NAMES UTF-8? – Kreator Jan 24 '13 at 23:30
  • yes, you need `SET NAMES ...` to access `$record['Име_и_презиме']` cyrillic field names and `echo "" . htmlspecialchars($record['Имејл']) . "";` to encode special html chars. – clover Jan 24 '13 at 23:32
  • Thx to all who participated in this topic and helped me, especialy clover. You all gave me lot of tips, so i now have to implement it to the scripts. If, in some case, it again doesnt work (which i doubt), ill will post that on this topic. cya – Kreator Jan 24 '13 at 23:37
  • Hello again, i changed the script acording to your advices and unfortunatly the same problem/error arives. I upd the code, you can see it after initial message, and also you can see warning that comes wheb i initilize script. – Kreator Jan 25 '13 at 21:38
  • Make shure your file has UTF-8 encoding. – clover Jan 31 '13 at 11:16