MySQL Forums
Forum List  »  PHP

PHP inserts gobbledygook into mySQL when data is not English
Posted by: Arthur Chan
Date: September 26, 2018 10:21AM

I am trying to upload data from a Java app to a mySQL database encoded in utf8mb4 for all columns.

The Java app calls a PHP script to execute a store procedure to insert the data.

I expect the app to insert data in English, Kanji, Chinese or Greek

What's working:
===============
Uploading and inserting English data work as expected.

What's not working
==================
Uploading and inserting Chinese, Kanji, Greek etc resulted in this goobledygook mess 美人鱼-αβργτ-ἄλφα

The Java app is working as it is capturing and passing on the data to PHP.

The mySQL sproc likewise is working b/c it is inserting the data into the database, whether it is English or gobbledygook.

So I assume the problem is with encoding in PHP, here is the basic code
snippet:
<?php
$con = mysqli_connect("localhost", "username", "password", "dbinstance");
if (mysqli_connect_errno($con))
{
   echo "Failed to connect to MySQL: ".mysqli_connect_error();
}
con->query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
$coyName= $_POST['coyName'];
$result = "call sp_newCompany('$coyName')";
if(! mysqli_query($con, $result) ) {
    die('Can not insert data: '.mysqli_error());
   } else {
  'New company $coyName inserted successfully<br/>\n';
}
mysqli_close($con);
?>

What I have tried sofar:
========================
I used JSON encode in Java app, then JSON_Decoe in PHP, but that did NOT resolve problem, I still got gobbledygook when I tried any language other than English.

Can someone pls point me in the right direction?

Maybe if I specify ASCII on upload from Java? I'll try that now.

Options: ReplyQuote


Subject
Written By
Posted
PHP inserts gobbledygook into mySQL when data is not English
September 26, 2018 10:21AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.