POST Cyrillic letters PHP results in special characters - php

Cyrillic letters are posting as spec symbols in db.
here is my php code
<?php
header('Content-Type: text/html; charset=utf-8');
include("../connection.php");
$data = json_decode(file_get_contents("php://input"));
$address = "Коля";
$description = $data->description;
$alt = $data->lat;
$lon= $data->lng;
$q = "INSERT INTO pitches (address, description,alt,lon) VALUES (:address, :description,:alt,:lon)";
$query = $db->prepare($q);
$execute = $query->execute(array(
":address" => $address,
":description" => $description,
":alt"=>$alt,
":lon"=>$lon
));
echo mb_detect_encoding($description);
?>
Also I've added .htaccess file to root of my site with AddDefaultCharset UTF-8
Here are my screenshots from phpmyadmin:
I have utf8 encoding everywhere but still my php posts me data like this КолÑ. I've tried everything I knew but can't understand what's wrong.
echo mb_detect_encoding($description) shows me ANSII,
echo mb_detect_encoding($address) shows me UTF-8
Will be really thankful for help.

Have you tried mysql set charset?
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Change character set to utf8
mysqli_set_charset($con,"utf8");
mysqli_close($con);
?>

Related

MySQL SELECT on Arabic Word returns 0 results on PHP but does on SQLBuddy/phpMyAdmin

Problem
I am getting 0 results when searching for an Arabic word in a MySQL database using the SELECT query with PHP.
However, the same exact query yields results in alternative clients, namely SQLBuddy and the likes. Everything is encoded in UTF-8.
Code
<?php
$host = "localhost";
$username = "hans_wehr_client";
// i know my security is a joke :)
$password = "hans_wehr";
$database = "hans_wehr";
$conn = new mysqli($host, $username, $password, $database);
if ($conn == TRUE){
$search = $_GET["search"];
$encoded_search = utf8_encode($search);
echo $encoded_search."<br>";
header('Content-Type: text/html; charset=utf-8');
$sql = "SELECT * FROM dictionary WHERE ARABIC LIKE '$search'";
echo $sql."<br>";
mysqli_query($conn,"SET NAMES 'utf8'");
mysqli_query($conn,'SET CHARACTER SET utf8');
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
header('Content-Type: text/html; charset=utf-8');
echo $row["ARABIC"]. " - Meaning: " . $row["ENGLISH1"]. " " . $row["ENGLISH2"]. "<br>";
}
}else {
echo "0 results";
}
}
?>
Before the mods get the pitchforks, I have to clear up my troubleshooting logic.
Encoding. I set the page encoding to utf-8 using header('Content-Type:
text/html; charset=utf-8'); and ran the queries mysqli_query($conn,"SET NAMES 'utf8'"); and mysqli_query($conn,'SET CHARACTER SET utf8');, this cleared up the ??????? and Ùؤتا
rendered instead of Arabic words issue. That is kind of a different
issue. Source. and Source2.
Database Charset. My database and columns are set to UTF-8.
Other clients work. SQLBuddy/MySQL native client/ PHPMyAdmin appear to be working because running the same exact query yields result. Therefore I appear to be on the same bloody boat with him. The query SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' returns a result on SQLbuddy but nada on PHP.
Possible solution:
Running the query SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' yields me a result.
However running the query with a UTF-8 encoded version of the Arabic word returns 0 results. SELECT * FROM dictionary WHERE ARABIC LIKE 'آخَر، أُخرى' I think this simulates PHP.
The UTF-8 Arabic word version is obtained by decoding the automatically URL encoded $[_GET] parameter i.e %26%231570%3B%26%231582%3B%26%231614%3B%26%231585%3B%26%231548%3B+%26%231571%3B%26%231615%3B%26%231582%3B%26%231585%3B%26%231609%3B
Could it be that the MySQLi actually queries the UTF-8 version instead of the actual Arabic word? Therefore finding no match since they are different?
If so how can I explicitly tell PHP not to URL encode my search term and therefore pass it as it is?
Since according to my tinfoil theory, http://localhost/hans_wehr/search_ar.php?search=آخَر، أُخرى would work but http://localhost/hans_wehr/search_ar.php?search=%26%231570%3B%26%231582%3B%26%231614%3B%26%231585%3B%26%231548%3B+%26%231571%3B%26%231615%3B%26%231582%3B%26%231585%3B%26%231609%3B
Inputs will be greatly appreciated.
Use html_entity_decode():
Use html_entity_decode() on your $_GET["search"] value
<?php
$host = "localhost";
$username = "hans_wehr_client";
// i know my security is a joke :)
$password = "hans_wehr";
$database = "hans_wehr";
$conn = new mysqli($host, $username, $password, $database);
if ($conn == TRUE){
$search = $_GET["search"];
$encoded_search =html_entity_decode($search, ENT_COMPAT, 'UTF-8');
echo $encoded_search."<br>";
header('Content-Type: text/html; charset=utf-8');
$sql = "SELECT * FROM dictionary WHERE ARABIC LIKE '$encoded_search'";
echo $sql."<br>";
mysqli_query($conn,"SET NAMES 'utf8'");
mysqli_query($conn,'SET CHARACTER SET utf8');
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
header('Content-Type: text/html; charset=utf-8');
echo $row["ARABIC"]. " - Meaning: " . $row["ENGLISH1"]. " " . $row["ENGLISH2"]. "<br>";
}
}else {
echo "0 results";
}
}
?>

Insertion and retrieving from MySQL returns ?????????????? sign for Arabic text [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
I have this insertion PHP code :
<?PHP
require("config.inc.php");
if (!empty($_POST)) {
$query = "INSERT INTO projects ( project_author, project_title, project_spec, project_links, project_desc,created_at ) VALUES ( :user, :title, :spec, :links, :desc, NOW() ) ";
$query_params = array(
':user' => $_POST['author'],
':title' => $_POST['title'],
':spec' => $_POST['spec'],
':links' => $_POST['links'],
':desc' => $_POST['desc'],
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add post!";
die(json_encode($ex));
}
$response["success"] = 1;
$response["message"] = "Post Successfully Added!";
echo json_encode($response);
}else{
echo 'omar almrsomi';
}
?>
The retrieving PHP code from MySQL is:
<?php
$mysql_db_hostname = "com";
$mysql_db_user = "xxxxx";
$mysql_db_password = "xxxxxxxxxx";
$mysql_db_database = "xxxxxxxxx";
$con = #mysqli_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password,
$mysql_db_database);
if (!$con) {
trigger_error('Could not connect to MySQL: ' . mysqli_connect_error());
}
$var = array();
$sql = "SELECT * FROM projects";
$result = mysqli_query($con, $sql);
while($obj = mysqli_fetch_object($result)) {
$var[] = $obj;
}
echo '{"projects":'.json_encode($var).'}';
?>
Problem:
The insertion and retrieving from MySQL gave ????????????? sign.
Note:
Inserted text in Arabic.
Question:
How to insert Arabic (Unicode) to MySQL?
Thanks in advance
You need to set the correct Collation (aka character encoding) on the table, as well as the Character Set on the table and/or Database as a whole to use which will have the needed arabic characters on the collation, otherwise unknown characters will appears as you have seen, as ? , I do not know which character encoding will be best for your chosen characters but take a look here for help saving Arabic characters to MySQL. Also check that you have the same encoding declared on your output HTML page else the page may try and render the characters with an incorrect default encoding, and giving you the same issue.
Store Arabic text in mysql database using php
Also as detailed by Chris85 in comments, UTF-8 all the way through is a very good link to read.

Posting Base64 encoded values in phpmyadmin from Android

In my app i am trying to post image from android to phpmyadmin i have also created php code which is here:
MyPhp.php:
<?php
$servername = "dontneedthis";
$username = "also";
$password = "dont care";
$dbname = "bla bla";
$conn =mysqli_connect('localhost', $username, $password);
$db_selected = mysqli_select_db($conn, $dbname);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}
$base=$_REQUEST['image'];
$filename = $_REQUEST['filename'];
$binary=base64_decode($base);
$sql = "INSERT INTO table (image) VALUES('{$binary}')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
When i upload image from android i get an error where it is shown that he doent understand this :
INSERT INTO table (image) VALUES
And he shows a lot of symbols which i do not recognise. I have created table where is a row where you can add 100 000 symbols of TEXT I tried to add the value as blob and tried to change collation to binary nothing worked do you have any ideas?
Why doesn't anyone ever bother to properly quote their stuff?
table is a keyword in all SQL dialects I know, and hence causes a syntax error.
But for that reason, quotes and backticks have been invented.
Do this:
INSERT INTO `table` (`image`) VALUES ...
and you should have one problem less.
Also, you have to escape your $binary variable, otherwise it's gonna break your ' quotes:
$binary = mysqli_real_escape_string($conn, base64_decode($base));

Accented characters appear in text file but not in terminal (osx)

I have been working on retrieving data from a database, I have been having trouble getting the accented characters to show (they appear as ? and I have tried many things to try and get this to work).
I just tried putting the data into a file using file_put_contents which means the issue might be with the terminal itself and not with the character encoding, unless I am wrong?
I then tried file_get_contents to read the file with the correct accented characters, and it still shows as ? in the terminal
Does anyone have any idea how I could get to show the data in the terminal with accented characters included? If I try to echo a simple é it shows up perfectly.
Thanks!
My code:
<?php
ini_set('default_charset','utf-8');
header('Content-Type: text/html; charset=utf-8');
$username = "username";
$password = "password?";
$dsn = "dsn";
$connection = odbc_connect($dsn, $username, $password, SQL_CUR_USE_ODBC);
$sql = "SELECT \"Insert ID\", \"Date Created\", \"Date Modified\", Description FROM Insert";
$res = odbc_exec($connection,$sql);
$x=0;
while ($row = odbc_fetch_array($res)){
$x++;
$values= ($x . ": " . " Insert ID:". $row['Insert ID'] . " Date Created: " . $row['Date Created'] . " Date Modified:" . $row['Date Modified'] . " Dscription:" . $row['Description'] . "\n");
print($values);
}
odbc_free_result($res);
odbc_close($connection);
Your PHP file is most likely saved as ISO Latin 1 or Mac OS Roman. Change it to UTF-8 and it should work. I just tried it on my Mac and it output 'special' characters no problem:
<?php
//test.php
$data = 'é';
file_put_contents('./test.txt', $data);
$output = file_get_contents('./test.txt');
echo $output.PHP_EOL;
Run the script in Terminal.app:
$ php test.php //outputs 'é'

PHP MYSQL encoding issue ( Turkish Characters )

core.php
$con=mysqli_connect("localhost","root","","mmogezgini");
header('Content-Type: text/html; charset=utf-8');
gamelisti.php
$result = mysqli_query($con,"SELECT * FROM games");
while($row = mysqli_fetch_array($result))
{
?>
HTML CODES
<?php echo $row['game_name']; ?>
HTML CODES
<?php }
mysqli_close($con);
?>
i use turkish characters like "ö,ç,ğ,ı" i see these correctly in database but when i select them from database and show with php echo they looks like question mark=(?)
my encoding on database utf8_general_ci
Double check your HTML encoding.
http://php.net/manual/en/mysqli.set-charset.php (just after you connect to DB: mysqli_set_charset($con, 'utf8');
Are you using prepared statements? (not related but important).

Categories