this is getting me mad.
This is the code I'm using right now:
<?php
//open connection to mysql db
$connection = mysqli_connect( "mysql.kit-series.net", "XXXXX", "XXXXXX", "kit_series_net" ) or die( "Error " . mysqli_error( $connection ) );
$connection->set_charset( "utf8" );
//fetch table rows from mysql db
$nombre = $_POST['nombre'];
$select = "SELECT ID FROM wp_qnnpx4_posts WHERE post_type='tvshows' AND post_title = '" . $nombre . "'";
$result = $connection->query( utf8_encode( $select ) );
$result = $result->fetch_assoc();
$id = $result['ID'];
echo $select;
echo $id;
//close the db connection
mysqli_close( $connection );
The result it's the expected if I use the select on console.
The result it's the expected if I use the select in the PHP and the
$nombre varible has no accents.
The result it's the expected if I use
the select in the console and the $nombre varible has accents.
Only when $nombre has accents and is used on the PHP file, the result isn't the expect, and it just return no results.
I printed the select in that case, put it on console, and it works. I dind't know what more to do.
Any idea?
Don't use utf8_encode unless you are processing non-UTF8 strings. From your question and comments it turns out that you are completely on UTF8 (HTML, database, database connection, PHP file encoding), so calling utf8_encode is not only not necessary, it will have averse effects.
As said in an appreciated contribution to the PHP docs on utf8_encode:
If your text is not encoded in ISO-8859-1, you do not need this function. If your text is already in UTF-8, you do not need this function. In fact, applying this function to text that is not encoded in ISO-8859-1 will most likely simply garble that text.
Related
I fetch cyrillic data from mssql with Php odbc,
it shown as question mark "?"
I use "Turkish_CI_AS" Collation and column type is NVARCHAR
Besides, on .net project has shown correctly that character
What should I do? Can anyone assist on me?
here is my PHP code example
<?
$dsn = "Driver={SQL Server};Server=XXX;Database=dtbs;SET NAMES 'utf8';SET CHARACTER SET utf8;MARS_Connection=yes";
$my_sql = odbc_connect($dsn, 'user', 'pass');
$query = "SELECT col FROM Tbl WHERE id=?";
$prepared = odbc_prepare($my_sql, $query);
if(!$prepared) die("could not prepare statement");
$params=array('1');
if(odbc_execute($prepared, $params)) {
$rows = odbc_fetch_array($prepared);
$col=$rows["col"];
echo $col;
}
?>
I tried many things for solution, but no result.
here is screenshot from SSMS
and screenshot from chrome
$content = mb_convert_encoding($col, 'utf-8', 'windows-1251');
echo $content;
I hope this solves your problem.
honestly I am coming here after trying everything I could find online in order to fix that problem but nothing worked..
I have a phpmyadmin database table that encoded to utf_general_ci, when I insert data in Hebrew into it it works fine and I can see the Hebrew in the table on PhpMyAdmin, but when I need to retrieve the Hebrew data from the table that's when the problem starts...
right now I am using a simple sql query to select data from the table and I tried every code that should fix it I could find online but nothing seems to work.
my current code is:
<?php
$db = "android";
$username = "root";
$password = "";
$host = "localhost";
$sql = "select * from sample;";
$conn = mysqli_connect($host,$username,$password,$db);
$conn->set_charset('utf8');
$result = mysqli_query($conn,$sql);
$response = array();
while($row = mysqli_fetch_array($result))
{
array_push($response,array($row[0],$row[1]));
}
$str = json_encode(array($response));
echo $str;
mysqli_close($conn);
?>
and the output of the Hebrew word is \u05d4\u05d9\u05d9 what seems like a different encoding, my php file is encoded to UTF-8 and I am using npp to write it.
please try helping me out on this one cause I couldn't find an answer
This is a json_encode() behaviour, by default Unicode char are escaped as \u...
Try with JSON_UNESCAPED_UNICODE flag :
<?php
echo json_encode($arr, JSON_UNESCAPED_UNICODE);
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";
}
}
?>
I'm a bit newbi in PHP. I implemented http://www.sanwebe.com/2013/03/loading-more-results-from-database solution in my new website: http://do2go.pl/do2go-nowa/
its working well - except encoding. My DB and all things are in UTF8. When fetch taking data from DB UTF8 seems not working.
Heres configuration and fetch code:
Config:
<?php
$db_username = 'kamio2_do2gonowa';
$db_password = 'JeremiasZ1!';
$db_name = 'kamio2_do2gonowa';
$db_host = 'localhost';
$item_per_page = 2;
mysqli_query ("SET NAMES 'utf8'"); mysqli_set_charset('utf8');
$connecDB = mysqli_connect($db_host, $db_username, $db_password,$db_name) or die('could not connect to database');
?>
And fetch:
<?php
include("config.inc.php"); //include config file
//sanitize post value
$page_number = filter_var($_POST["page"], FILTER_SANITIZE_NUMBER_INT, FILTER_FLAG_STRIP_HIGH);
header('Content-Type: text/html; charset=UTF-8');
//throw HTTP error if page number is not valid
if(!is_numeric($page_number)){
header('HTTP/1.1 500 Invalid page number!');
exit();
}
//get current starting point of records
$position = ($page_number * $item_per_page);
//Limit our results within a specified range.
$results = mysqli_query($connecDB, "SELECT id,introtext FROM w7wst_content ORDER BY id DESC LIMIT $position, $item_per_page");
//output results from database
echo '<ul class="page_result">';
while($row = mysqli_fetch_array($results))
{
echo '<li id="item_'.$row["id"].'"><span class="page_message">'.$row["introtext"].'</span></li>';
}
echo '</ul>';
?>
I added mysql_query ("SET NAMES 'utf8'"); mysql_set_charset('utf8'); but this does nothing. Still getting � symbols and ? instead of " All next 3 was changed directly in DB tables for html symbols which isn't a solution.
Any help appreciated! :)
(To close the question, since this was the solution to the OP's problem)
Add $connecDB->set_charset("utf8"); just before $results = mysqli_query($connecDB...
You are using mysql and mysqli in one script. Only use mysqli as mysql has been deprecated. That said try this:
mysqli_character_set_name($connectDB);
mysqli_set_char_set($connectDB, 'UTF8');
Set these after your connection script.
Did you check if they are properly stored in the table? Try typing this in your database(not in PHP):
SELECT * FROM w7wst_content;
and see if it's displaying properly. Alternatively you can check table contents with some tool like phpmyadmin. If it's not you have to set collation to something like utf8_general_ci.
EDIT: Also, how about setting the charset AFTER connecting to the database, hm? :)
I have some of the fields in the database(MS SQL Server) which at times have the Japanese data/characters as well. When I retrieve them using php, the data turn into ???? Such as I got a field Model which stores "BMW and JAPANESE CHARACTER" and when it comes to the page it turn into "BMW and ????????"
Below is my code;
include ('org_dataDSN.php');
//Setting up database virtual connection
echo "Connecting Database <br>";
header( 'Content-Type: text/html; charset=utf-8' );
echo "Successfully connected....";
$subQuery="select model, make from infChnge_CS2002";
$subRes=odbc_exec($connect, $subQuery);
$ix=odbc_num_rows($subRes);
echo "Success.." . $ix;
while(odbc_fetch_row($subRes))
{
$cstate = odbc_result($subRes, 1);
$sname = odbc_result($subRes, 2);
echo $cstate . "<br>";
echo $sname . "";
}
odbc_close($connect);
?>
First cast the data in the MSSQL query using cast function
SELECT CAST(model AS TEXT) as model, CAST(make AS TEXT) as make, FROM r_table;
In php, convert the casted data to utf-8:
$model = iconv('CP1255', 'UTF-8', $model);
$make = iconv('CP1255', 'UTF-8', $make);
i used this for mysql connection to correct ????? in Persian Language
$connect=mysql_connect('localhost','root','');
mysql_query("SET NAMES 'utf8'", $connect);
query this "SET NAMES 'utf8'" right after creating connection to server