Hello! I have an application for android that interacts with the database. api is written on pkhp. I did not learn pkhp, but redid the code to fit my needs, in principle everything works. but the problem is with the Russian language. When I do the registration appear in the fields of the database such characters "????".

Here is the code:

<?php class DB_Functions { private $db; //put your code here // constructor function __construct() { require_once 'DB_Connect.php'; // connecting to database $this->db = new DB_Connect(); mysql_query("SET NAMES utf8"); $this->db->connect(); } // destructor function __destruct() { } /** * Storing new user * returns user details */ public function storeUser($name, $email, $password,$login,$phone,$street,$house,$country,$town,$region,$pol,$age) { $uuid = uniqid('', true); $hash = $this->hashSSHA($password); $encrypted_password = $hash["encrypted"]; // encrypted password $salt = $hash["salt"]; // salt $result = mysql_query("INSERT INTO ****(unique_id, name, email, encrypted_password, salt, created_at,login,phone,street,house,country,town,oblast,pol,age) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW(),'$login','$phone','$street','$house','$country','$town','$region','$pol','$age')"); // check for successful store if ($result) { // get user details $uid = mysql_insert_id(); // last inserted id $result = mysql_query("SELECT * FROM **** WHERE uid = $uid"); // return user details return mysql_fetch_array($result); } else { return false; } } /** * Storing new user * returns user details */ public function storeDriver($name, $email, $password,$login,$phone,$street,$house,$country,$town,$region,$pol,$age,$marka,$model,$gosnumber,$color) { $uuid = uniqid('', true); $hash = $this->hashSSHA($password); $encrypted_password = $hash["encrypted"]; // encrypted password $salt = $hash["salt"]; // salt $result = mysql_query("INSERT INTO ****(unique_id, name, email, encrypted_password, salt, created_at,login,phone,street,house,country,town,oblast,pol,age,auto,auto_model,gosnumber,color) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', NOW(),'$login','$phone','$street','$house','$country','$town','$region','$pol','$age','$marka','$model','$gosnumber','$color')"); // check for successful store if ($result) { // get user details $uid = mysql_insert_id(); // last inserted id $result = mysql_query("SELECT * FROM **** WHERE uid = $uid"); // return user details return mysql_fetch_array($result); } else { return false; } } /** * Get user by email and password */ public function getUserByEmailAndPassword($email, $password) { $result = mysql_query("SELECT * FROM **** WHERE email = '$email'") or die(mysql_error()); $result1 = mysql_query("SELECT * FROM **** WHERE email = '$email'") or die(mysql_error()); // check for result $no_of_rows = mysql_num_rows($result); $no_of_rows1 = mysql_num_rows($result1); if ($no_of_rows > 0) { $result = mysql_fetch_array($result); $salt = $result['salt']; $encrypted_password = $result['encrypted_password']; $hash = $this->checkhashSSHA($salt, $password); // check for password equality if ($encrypted_password == $hash) { // user authentication details are correct return $result; } } else if ($no_of_rows1 > 0) { $result1 = mysql_fetch_array($result1); $salt = $result1['salt']; $encrypted_password = $result1['encrypted_password']; $hash = $this->checkhashSSHA($salt, $password); // check for password equality if ($encrypted_password == $hash) { // user authentication details are correct return $result1; } } else { // user not found return false; } } /** * Check user is existed or not */ public function isUserExisted($email) { $result = mysql_query("SELECT email from **** WHERE email = '$email'"); $no_of_rows = mysql_num_rows($result); if ($no_of_rows > 0) { // user existed return true; } else { // user not existed return false; } } /** * Check driver is existed or not */ public function isDriverExisted($email) { $result = mysql_query("SELECT email from **** WHERE email = '$email'"); $no_of_rows = mysql_num_rows($result); if ($no_of_rows > 0) { // user existed return true; } else { // user not existed return false; } } /** * Encrypting password * @param password * returns salt and encrypted password */ public function hashSSHA($password) { $salt = sha1(rand()); $salt = substr($salt, 0, 10); $encrypted = base64_encode(sha1($password . $salt, true) . $salt); $hash = array("salt" => $salt, "encrypted" => $encrypted); return $hash; } /** * Decrypting password * @param salt, password * returns hash string */ public function checkhashSSHA($salt, $password) { $hash = base64_encode(sha1($password . $salt, true) . $salt); return $hash; } } ?> 

that's how I sent the data

 public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { // Making HTTP request try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); Log.e("JSON", json); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } 

PS even mysql_query ("SET NAMES utf8"); Does not help

  • depricated This extension is deprecated since PHP 5.5.0 and will be removed in the future. Use MySQLi or PDO_MySQL instead. See also the MySQL instruction: API selection and the corresponding FAQ for more details. Alternatives to this function: mysqli_connect () PDO :: __ construct () - zb '18
  • I wrote mysqli instead of mysql everywhere and nothing gives an error. previously worked at least. By the way, the notepad ++ does not highlight mysqli in blue, probably it is really something new. - Firespirit

1 answer 1

[problem solved]

everything was in the base with the base. you just need to transfer the php files to utf8-without BOM and mysql_query ("SET NAMES utf8"); register after connecting to the database

The problem was in java. it was necessary to set the encoding in the http parameters, here is the code of the class of the parser, can someone come in handy.

 package com.library; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.HttpVersion; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpParams; import org.apache.http.params.HttpProtocolParams; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; import android.util.Log; public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; // constructor public JSONParser() { } public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) { // Making HTTP request try { // defaultHttpClient HttpParams httpParams = new BasicHttpParams(); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParams, "UTF-8"); HttpProtocolParams.setHttpElementCharset(httpParams, "UTF-8"); httpParams.setBooleanParameter("http.protocol.expect-continue", false); HttpClient httpClient = new DefaultHttpClient(httpParams); httpClient.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1); httpClient.getParams().setParameter("http.socket.timeout", new Integer(2000)); httpClient.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { BufferedReader reader = new BufferedReader(new InputStreamReader( is, "UTF-8")); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); Log.e("JSON", json); } catch (Exception e) { Log.e("Buffer Error", "Error converting result " + e.toString()); } // try parse the string to a JSON object try { jObj = new JSONObject(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } // return JSON String return jObj; } }