0

I have a C#-Application which sends data to a php and the php stores the data into a mysql-database. And if I have an äöü or some other stuff (I´m urlencoding it, so there are also &%$/")=! contained in the string) it will be stored in the database as È└/ or something like that. So it´s definely an encoding-problem. How can I solve it?

EDIT:
I´m using these pieces of code:
C#:

WebClient.DownloadString(apiString + "sendMessage.php?user=" + user + "&message=" + message);

PHP:

$verbindung = mysql_connect("nightking.org", "username", "password");
mysql_select_db("apidb");
$ergebnis = mysql_query("INSERT INTO `apidb`.`tm_chatdata` (`username` ,`time` ,`message`) VALUES ('" . $_GET['message'] . "',CURRENT_TIMESTAMP , '" . $_GET['user'] . "');");

When I call the values again:

PHP:

$verbindung = mysql_connect("nightking.org", "username", "password");
mysql_select_db("apidb");
$ergebnis = mysql_query("(SELECT * FROM `tm_chatdata` ORDER BY time DESC LIMIT 38)ORDER BY time ASC;");
$firstOne = true;
while($row = mysql_fetch_object($ergebnis))
{
    if($firstOne)
        $firstOne = false;
    else
    {
        echo "\r\n";
    }
    if($row->username == "system")
    {
        echo "[" . $row->message . "]";
    }
    else
    {
        if($row->username == "command")
        {
            echo "*".$row->message;
        }
        else
        {
            echo /* $row->time . " " . */$row->username . ": " . $row->message;
        }
    }
}

C#:

WebClient.DownloadString(apiString + "getMessages.php");

The MySQL collation type is now changed to utf8_bin and it still does not work.

2
  • Where are you doing URL-encoding and why? You only (generally) need to URL-encode text if they're going into a URL. Commented Oct 19, 2013 at 10:43
  • I´m sending the data via get to a php-file Commented Oct 19, 2013 at 11:16

1 Answer 1

2

You'll need to audit the path of which data in your application goes through.

Starting in C# land, you'll probably be using HttpWebRequest to send data to your PHP script. If you're only setting querystring or HTTP header values then you don't need to encode anything: HttpWebRequest will do that for you. If you're setting POSTdata using request.GetRequestStream then it depends on what kind of form submission you're doing, though HTML5 states by default this should be UTF-8 with non-ASCII characters converted to numeric values. Note that this only concerns over-the-wire format, the PHP environment will automatically handle this for you.

PHP does not have built-in support for Unicode: all strings can be considered as arrays of single-bytes, thus provided they are not manipulated then they will preserve data on a per-byte basis. For manipulation, use the mbstring extension.

MySQL presents another issue as it allows per-table encoding. The default is latin1 though I'd recommend setting this to utf8 or utf16 for the sake of consistency. Even worse: you have to contend with connection encoding too.

So there really aren't any simple answers, but I would suggest making sure that everything is consistent:

  • Ensure that your method of encoding (both the form-encoding and the actual 'numerical encoding' of characters) is declared in your HTTP client in your C# application.
  • Ensure you aren't manipulating multibyte strings in an unsafe manner in PHP and that you're handling them in a way consistent with the encoding declared in the HTTP headers of the incoming request by the C# program.
  • Ensure that your MySQL tables are all using the same encoding and it's an appropriate encoding (e.g. utf8 or utf16) and that if the incoming-data has a different encoding then you'll convert it accordingly before saving it to the database.
Sign up to request clarification or add additional context in comments.

2 Comments

I´m using WebClient.DownoadString()
+1 Because your answer isright, but I still don´t know how to solve it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.