0

I've tried inserting and reading from my MySQL database, but it doesn't seem to work. Here's what I've read, and tried:

Other SO posts:

http://stackoverflow.com/questions/742205/mysql-alter-table-collation/5468980#5468980

http://stackoverflow.com/questions/5906585/change-database-collation

Tried changing collation to utf-unicode-ci and character set to UTF-8, didn't work

http://stackoverflow.com/questions/9401567/php-chinese-characters-cant-insert-to-mysql

This is more for insertion, but reading is equally as problematic.

Here are screenshots of what happened:

Insertion error

https://i.stack.imgur.com/2ot0L.png

Display error

https://i.stack.imgur.com/Bndz1.png

Table structure

https://i.stack.imgur.com/XO3K5.png

Code snippets for writing Chinese to database (registering a new user):

registration.js

var postData = {
    full_name   :   $("#full-name").val(),
    email       :   $("#email").val(),
    password    :   $("#password").val(),
    username    :   $("#username").val()
};

$.ajax({
    type        :   'POST',
    url         :   '../php/register.php', 
    data        :   postData,
    dataType    :   'JSON',
    success     :   function(data) {
        if (data.result == -1)
            alert('Registration unsuccessful!');
        else if (data.result == 0) {                // redirect to page
            alert('Registration successful!');
            window.location = '../home.php';
        }
    }
});

registration.php

$query = "  INSERT INTO `users`(`Username`, `Display_name`, `Password`, `Email`, `Date created`) 
            VALUES ('{$username}', '{$display_name}', '{$password}', '{$email}', '{$date}')";

if ( ($result = mysqli_query($link, $query)) == false ) 
    return -1;
else
    return 0;

Code for reading Chinese from database (displaying user name):

functions.php

$query = "SELECT display_name, followers FROM users ORDER BY followers DESC LIMIT 10";
$result = mysqli_query($link, $query);

while ($row = mysqli_fetch_array($result)) {
    $html = "<li>{$row['display_name']}<span id = 'popular-user-followers'>{$row['followers']}</span></li>";
    echo $html;
}

Would appreciate any advice on how to proceed, I'm not entirely familiar with databases yet. Thanks!

Code Apprentice
  • 522
  • 2
  • 7
  • 19

0 Answers0