SELECT statement doesn't work with utf-8 [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 1 year ago.
I have a select statement which should select all data in the database which equals to a Arabic input, I've set database's collation to utf8_general_ci and tables column as them for sure. And at the file
$conn = mysqli_connect($mysql_host, $mysql_user, $mysql_password, $mysql_database);
mysqli_query($conn, "set names 'utf8'");
mysqli_query($conn, "SET character_set_results=utf8");
mb_http_output('UTF-8');
mb_internal_encoding('UTF-8');
mb_http_input('UTF-8');
mb_language('uni');
mb_regex_encoding('UTF-8');
ob_start('mb_output_handler');
mb_language('uni');
mb_internal_encoding('UTF-8');
mysqli_set_charset($conn, "utf8");
And the code of the search
$question = $_REQUEST['question'];
$table = $_REQUEST['subject'];
if(empty($question)) {
echo "Type a question!"; }
$qe = mysqli_escape_string($conn, $question);
$full = "SELECT * FROM $table WHERE question LIKE '%$qe%'";
$fullQ = mysqli_query($conn, $full);
if(!$fullQ) {
echo mysqli_error($conn);
}
echo "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>";
printf(nl2br("\n%u\n"), mysqli_num_rows($fullQ));
while($row = mysqli_fetch_assoc($fullQ)) {
printf(nl2br("\n%s\n"), $row['question']);
}
It works if I print all the data without using WHERE and it also prints the SQL syntax after it is sent from the first page to the second one right with the correct Arabic characters. But it always return 0 rows.

If your database saved in arabic then set up connection string to the following
$con1 = mysqli_connect($HOSTURL,$DBUSER,$DBPASSWORD,$DBNAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die('');
}
mysqli_query($con1,"SET NAMES utf8");
//else{echo "Connection Done";}
if your database is not encoded then you do not have to set utf8 in your connection string.

Related

PHP returns symbols instead of scandics in result from MySql - although I'm using setcharset "latin1" [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 2 years ago.
I have this code:
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset($conn,'latin1_swedish_ci');
echo mysqli_character_set_name($conn);
$sql = "SELECT * FROM ggg";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "keyword: " . $row["keyword"]. " - Name: " . $row["firstname"]. " " . $row["lastname"] . $row["info"] . "<br>";
}
} else {
echo "0 results";
}
The $row["info"]-parts in the echoed result are full of questionmarks inside a rectangle instead of the swedish signs "å, ä, ö". Although the charset is correctly set to "latin1_swedish_ci" which is exactly the same as in the database (which I checked from phpMyAdmin).
What to try next, anyone have any ideas?
Firstly you may want to use utf8 or utf8mb4 unless you have a reason not to.
My guess is what you are seeing is not really a problem that comes out of the database, but its from rendering in the browser which does not know how to display what you are providing.
You may verify if this is the case in chrome using an extension, or in other browsers in other ways.
Alternately, set the document headers in HTML so the browser dont have to guess :)

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";
}
}
?>

getting error while fetch "hindi" results from database using php

I am trying to fetch "hindi" (utf-8) results from MySQL database using php.
I have written the following code..
<?php
$connection = mysql_connect("localhost","root","root");
if(!connection)
{
die('Could not connect: ' . mysql_error());
}
mysql_query('SET character_set_results=utf8');
mysql_query('SET name=utf8');
mysql_query('SET character_set_client=utf8');
mysql_query('SET character_set_connection=utf8');
mysql_query('SET character_set_results=utf8');
mysql_query('SET collation_connection=utf8_general_ci');
mysql_select_db('data',$connection);
$result = mysql_query("SET NAMES utf8");
$cmd = "select * from question";
$result = mysql_query($cmd);
$myArray = array();
while($row =$result->fetch_object()){
$tempArray = $row;
array_push($myArray, $tempArray);
}
echo json_encode($myArray);
?>
But I am not getting the expected results. I want to print Utf-8(Hindi) data.
if I use the following code to print individual row then
while($myrow = mysql_fetch_row($result)){
echo ($myrow[0]);
echo ($myrow[1]);
echo ($myrow[2]);
}
It works fine and prints the utf-8(Hindi) data.
Please help me what I am missing or is there anything wrong implemented....
Thanks in advance..!!
mysql_query('SET name=utf8');
Should be mysql_query('SET NAMES utf8');. The other SETs are not needed.
Also, don't use the deprecated mysql_* interface, rewrite to use mysqli_* (or PDO).
By Hindi, do you mean DEVANAGARI, such as हिन्दी ?
The utf8 hex for that is: E0A4B9 E0A4BF E0A4A8 E0A58D E0A4A6 E0A580
If you see something like हिनà¥à¤¦à¥€, you have "Mojibake".
not getting the expected results
What are you getting?
json_encode -- should include a second argument of JSON_UNESCAPED_UNICODE to avoid getting the "html entities".
To see hex in PHP,
$hex = unpack('H*', $text);
echo implode('', $hex);
latin1 will not suffice; you need utf8 (or utf8mb4).
I realize this is not a full or coherent answer, but it presents several action items for the OP to get closer to the solution.

Query fail with no error

I have a database UTF8 encoded, because it contains arabic letters. I have a php file to select all values from a given table:
<?php header('Content-type: application/json; charset=utf-8');
$host = "localhost"; //Your database host server
$db = "kalimat"; //Your database name
$user = "root"; //Your database user
$pass = ""; //Your password
$connection = mysqli_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
die(mysqli_error($db));
}
else
{
//Attempt to select the database
$dbconnect = mysqli_select_db($connection, $db);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$auteur=utf8_decode(urldecode($_GET["artist"]));
//$auteur=utf8_decode($auteur);
$resultset = mysqli_query($connection,"SELECT * FROM $auteur");
if( $resultset )
{
$records = array();
//Loop through all our records and add them to our array
while($r = mysqli_fetch_assoc($resultset))
{
$records[] = $r;
}
$records = array_map(function($r) {
$r['artist'] = utf8_encode($r['artist']);
return $r;
}, $records);
//Output the data as JSON
echo json_encode($records);
}
else{
die("Error:".mysqli_error()); //Here is the error
}
}
}
?>
In output, I have "Error:" without any specification, so I can't know the source of the problem.
Any something strange in the php file?
Thank you for your help.
have you tried utf8mb4 ?
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
utf8mb4 is superset of utf8
Advice: database ,table names and columns you should make them in non utf8 characters.
EDIT:
to handle with utf8 characters you have to make all in utf8 such as:
your files , convert them to utf8 , all pages where you are using utf8 also that page where you geting the author fom.
database , tables , columns.
your editor where you write codes.
headers.
...
try putting the following lines just after the php tag starts and error will be displayed.
error_reporting(E_ERROR);
ini_set("display_errors",1);
hope this helps.
You are using mysqli, not mysql. So you have to use appropriate error function, and call it according to it's proper syntax.
Also, your idea on storing data in user-defined tables is wrong. You have to learn relational database basics first.

Arabic not supported in mysql and php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have this part of code that works fine only the arabic issue.
$result = mysql_query("SELECT login, password, name, role FROM qm_users WHERE login = '$login'");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
$row = mysql_fetch_array($result);
echo $row["name"];
The name row contains arabic name.
The output of echo is ???? ???? ????
In my myphpadmin of xamp server, here the structure
Check below possible solution,
1) Your table structure 'Collation' must be 'utf8_unicode_ci'.
2) Also try to set below thing in php file
ini_set('default_charset','utf-8');
mysql_set_charset('utf8');
header('Content-type: text/html; charset=utf-8');
If you are going to save UTF8 encodings to your MySQL database first set your connection encoding to UTF8.
<?php
$connection = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
mysqli_set_charset($connection, "utf8");
?>

Categories