0

Right now i try to create php english vocabulary excercise on XAMPP

this is my code

  $correct_answer = get_correct_answer_by_id($question['id']);
$wrong_answer = get_wrong_answer_by_unit($lesson_number);

$all_answer[] = $correct_answer;    
while($w_ans = mysqli_fetch_assoc($wrong_answer))
{
$all_answer[] = $w_ans;
}

echo '<pre>';
var_dump($all_answer);
echo '</pre>';

and this is result

array(4) 
  {
  [0]=>
  array(5) {
    ["id"]=>
    string(1) "4"
    ["vocab"]=>
    string(7) "erosion"
    ["unit"]=>
    string(1) "1"
    ["answer"]=>
    string(24) "เธเธฑเธ”เธเธฃเนเธญเธ"
    ["position"]=>
    string(5) "(n)
  }

i don't know why answer = "เธเธฑเธ”เธเธฃเนเธญเธ" it should be "กัดกร่อน"

but if i didn't have this line

$all_answer[] = $correct_answer; 

OR

while($w_ans = mysqli_fetch_assoc($wrong_answer))
{   
$all_answer[] = $w_ans;
} 

if i only use one of those command not both it didn't had any problems. And i didn't know why? May be someone please help me.

Varis Darasirikul
  • 3,907
  • 9
  • 40
  • 75
  • What are `$correct_answer` and `$wrong_answer`? Are they arrays? If you do a query for each one are you sure that the result of your query is correct? – Javad Mar 10 '14 at 21:38
  • Since you don't show the DB query that's actually fetching this data, we can't help you. There's NOTHING in this code that could explain why you get the wrong data, other than maybe your query being wrong. – Marc B Mar 10 '14 at 21:48

1 Answers1

0

if i only use one of those command not both it didn't had any problems.

You have two strings which are stored using different byte encodings. One of them is is UTF-8; the other is in code page 874 (Windows legacy Thai). เธเธฑเธ”เธเธฃเนเธญเธ is what you get when you take the string กัดกร่อน encoded in UTF-8 and misinterpret it as being in cp874.

You are producing an output page that has no specified encoding. In this case the browser will do its best to guess what encoding you might be using, which is pretty unreliable. In your case it detects UTF-8 when the page is valid interpreted as UTF-8, and cp874 when it isn't. Presumably your browser is running in a Thai locale; other browsers would display different nonsense.

A whole page has to have a single encoding, so you can't combine strings using two encodings on the same page. When you include both, your browser sees that there is some non-valid-in-UTF8 content so it falls back to cp874, which renders the material in 874 correctly, but the material in UTF-8 as nonsense.

What you need to do is make sure everything about your application uses the same encoding explicitly. For sanity this encoding should be UTF-8. Include a <meta charset="utf-8"/> at the top of your <head>. Save any files that contain non-ASCII characters in UTF-8 format. (This is sadly not the default in Windows applications like Notepad. My guess is that this is your problem, although it's not possible to tell for sure without more context.) Store your database tables in UTF-8, and talk to your database in UTF-8.

UTF-8 all the way through

Community
  • 1
  • 1
bobince
  • 528,062
  • 107
  • 651
  • 834