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

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 'é'

Related

POST Cyrillic letters PHP results in special characters

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);
?>

php 5.6 - connecting at oracle DB using pdo and specify charset/encoding

I'm not sure what i could be missing, but currently with this working code I'm able to connect, query and return the results from the db table properly.
But how could show the results from the table with the correct encoding? since special chars like ç, á are being displayed sometimes like question mark or a square box.
Bellow, all the information that i have so far, but I believe definition must be set on the PDO because if i simply do an echo "ç -á"; on the page, the chars will be displayed properly.
Oracle 11 Table charset:
Query: SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET' ;
Result: WE8MSWIN1252
php.ini file encoding settings:
default_charset = "UTF-8"
internal_encoding = "UTF-8"
input_encoding = "UTF-8"
output_encoding = "UTF-8"
Code example:
#
# PDO Connect example
#
$tns = "
(DESCRIPTION=
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
)
(CONNECT_DATA=
(SID=the_database)
)
)
";
$username = "x";
$password = "x";
try {
$conn = new PDO("oci:dbname=" . $tns, $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
#
# Fetch results and build html table
#
$sql = "select * table";
$result = $conn->query($sql);
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th>Info</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach ($conn->query($sql) as $row) {
echo '<tr>';
echo '<td >' . $row['INFO'] . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
Adding :charset=utf8 to the end of your DSN string should result in:
the database session being created with UTF-8 as the session encoding
the data is stored as Windows-1252, based on that NLS WE8MSWIN1252 setting
Oracle will try to convert the raw Windows-1252 value in the column into an equivalent UTF-8 string as requested by your database session's UTF-8 setting
if and only if that raw Windows-1252 raw byte value correctly matches a valid UTF-8 character (which I'm pretty sure all 1252 chars have valid UTF-8 matches), then the value returned should be visible to you as the UTF-8 character
Since you said that a raw echo of those special characters does appear correctly in your browser, then most likely your "viewer" settings (browser) are such that echoing the returned UTF-8 characters should succeed.

Problems with swedish letters MySQL and LAMP

I have struggled with this some time. I could not get it to work.
On the url http://course.easec.se/problem.pdf I have put together what I have done so far!
Any suggestions?
<?php
header('Content-Type: text/html; charset=ISO-8859-1'); //no changes
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "db_test4";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
mysqli_set_charset("utf8");
echo "From PHP Script ååååå\n"; //to see if there is problem when edit the script
$sql = "SELECT * FROM test_tbl";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "Kundid: " . $row["kund_id"]. " - Förnamn: " . $row["fnamn"]. " - Efternamn: " . $row["enamn"]. " - Adress:" . $row["adress1"] ."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
Client:
It works now, seems do be my editor, when I tried ANSI instead of UTF-8 and Changes the header to !
enter image description here
Directly below
$conn = new mysqli($servername, $username, $password, $dbname);
add
$conn->set_charset("utf8");
Note: In addition you should also make sure that the charset of your HTML (in the <head> tag) is set to utf-8, like <meta charset="utf-8">
Use:
header('Content-Type: text/html; charset=ISO-8859-1');
Instead of:
header('Content-type: text/html; charset=utf-8');
No need to use inside while loop, use top of the page before print anything.
I like how you set your question in a PDF, somewhat original :-D . Now, the answer to your problem will be found here. Read the whole question and as many of the answers as you can.
then as a last effort I change editor to ANSI instead of UTF-8 then it works!
Due to this the issue is your internal PHP script encoding so Reaseach using PHP Multibyte String and setting your PHP code to :
<?php
mb_internal_encoding('UTF-8'); //this is the important one!
mb_http_output('UTF-8');
mb_http_input('UTF-8');
to ensure your PHP code is also UTF-8 complient when executed. You can also (or should be able to) edit the settings in your IDE editor to save PHP files without UTF-8 BOM (Byte Order Mark) which will also solve this problem and save them as UTF-8 understandable by other services.
Additional Notes (cos I like to moan about MySQL UTF-8):
MySQL UTF-8 is NOT the full UTF-8 . Solutions are offered in the linked thread above, some general additions:
1) You want to always use UTF8mb4 (and ideally _unicode_ci).
2) In the final screenshot of your PDF you should to set as many of these as practical to utf8mb4_unicode_ci.
3) Setting the connection character set is vital, even if the server and the client both use utf8mb4 if the connection is only utf-8 it brings everything else down to that low point.
4) Recommended:
header('Content-Type: text/html; charset=UTF-8'); //UTF-8
Only MySQL UTF-8 is broken, other implementatios of it is not and is perfectly usable as and is generally regarded as an HTML standard.

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

Why my php script outputs incorrect symbols instead russian letters from sqlite3?

I'm trying to work with sqlite db from php 5.3 using sqlite3.exe on my windows computer.
when I am executing following code:
header('Content-Type: text/html; charset=utf-8');
$db = new SQLite3('C:/PHP5/extras/db.sqlite');
$result = $db->query('select * from comp;');
$resx = $result->fetchArray(SQLITE3_ASSOC);
while ($resx != false){
print_r($resx); echo "<br/>";
$resx = $result->fetchArray(SQLITE3_ASSOC);
};
It outputs �� symbols if text in Russian. But in sqlite CLI I see the letters 'АБ'.
I tried $db->exec('PRAGMA encoding = "utf-8";'); but it isn't help. What should I do to see the russian text from DB?

Categories