SQL query with accented characters [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 4 years ago.
I migrated a project to a new host. So I transferred my files and my database. But since then, my sql queries with accented characters no longer work (NULL):
php file:
$ product = 'Gestion réseaux sociaux';
$ handle-> query ('SELECT social_networks FROM sale WHERE id_customer = "'. $ id. '" AND product = "'. $ product. '"');
In addition, the accented characters displayed in the view from the database are not displayed correctly.
However, the rest of the written text content that does not come from the database displays the accented characters.
I still put the meta:
<meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8" />
So I deduce that it comes from the encoding of the database but it is identical to that of my former host (UTF8_unicode_ci)
What can be the reason for you?
Thank you in advance for your help !!

It was enough to add "charset=utf8" to PDO
new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=utf8", $this->user, $this->password);

Related

Updating MySQL table with non-english characters not working, collation and charset are all set to utf8 or utf8mb4 [duplicate]

This question already has answers here:
Trouble with UTF-8 characters; what I see is not what I stored
(5 answers)
UTF-8 all the way through
(13 answers)
Closed 2 years ago.
I am having an issue trying to update my database, I believe related to a charset / collation issue. I've searched all the other related issues, updated collations and charsets, tried everything, and nothing seems to work.
What I'm doing is getting data from scraping some HTML (with permission from the site owner), manipulating it a bit, and then doing an UPDATE to save the manipulated data in my table.
I have a field, reference, that is taken from the HTML, and the update looks for that field, and updates my table if the field matches. If there are no special (non-english) characters, it works fine:
UPDATE database.table SET points = 100 WHERE reference = 'Real Madrid'
If there are any non-english characters in the reference, then the update doesn't work IF I do it from my PHP / HTML site - if I put the query below directly into phpmyadmin, it works fine:
UPDATE database.table SET points = 100 WHERE reference = 'Atlético Madrid'
This happens with every non-english character I've tried, not just é, so that seems to be the root issue.
The HTML I ingest is initially UTF-8, but at some point, it seems the encoding of my text is getting changed from straight UTF-8 to ASCII. Is ASCII not a subset of UTF-8? Not entirely sure if that's the problem, but the encoding is different, which is odd.
Below is my code, with encoding pointed out at different times:
$html = file_get_html('http://url.to.scrape');
// At this point, `mb_detect_encoding($html)` is UTF-8.
$i = 1;
while($i <= 20){
foreach($html->find('tr') as $tableRow) {
// At this point, `mb_detect_encoding($tableRow) is `ASCII`
$rowData['team'] = $tableRow->find('td', 0)->plaintext;
// At this point, `mb_detect_encoding($rowData['team']) is `ASCII`
$rowData['points'] = $tableRow->find('td', 1)->plaintext;
$points = $rowData['points'] * doSomeManipulationHere();
$update_query = "UPDATE database.table SET points = $points WHERE reference = '". $rowData['team'] ."'";
print_r($update_query);
}
}
As mentioned, if $rowData['team'] does not contain non-english characters, it works. If it does contain any, it doesn't.
Again, as mentioned, If I print_r($update_query), and I copy / paste the output directly into phpmyadmin in the SQL tab, it works as expected, even with the é character, so that makes me believe that MySQL charset / collation is set up correctly, and it's somewhere in the PHP / HTML / MySQL connection that's causing the issue.
I guess I need to figure out why my data is suddenly ASCII when is started out as UTF-8.
My setup:
MySQL Server connection collation: utf8mb4_unicode_ci
MySQL Table collation: utf8mb4_unicode_ci
MySQL Field collation: utf8mb4_unicode_ci
PHP default Charset: UTF-8
HTML: <meta charset="utf-8">
.htaccess / charset.conf: AddDefaultCharset UTF-8 (edit: added after originally posted, thanks for the suggestion #asiri)
I've tried sending header('Content-Type: text/html; charset=utf-8');, which didn't help.
I am also seeing the dreaded black question mark � when I view those characters on the site, so it's gotta be the encoding somewhere, I just don't know where.
You can try setting up encoding in your .htaccess file.
AddDefaultCharset utf-8
Add this line into the .htaccess file inside the root directory where you have placed your code.
or
try this. The below code will set the encoding into your database connection.
$link = mysqli_connect('localhost', 'user', 'password', 'database');
mysql_set_charset('utf8',$link);

Querying by a php a table containing emoji returns question marks [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I am trying to retrieve a number of rows from a mysql DB, some of which containing emojis, through the following function:
<?php
ini_set('mssql.charset', 'UTF-8');
//$mysqli->query("SET NAMES 'utf8'");
$mysqli->query("SET character_set_client=utf8mb4");
$mysqli->query("SET character_set_connection=utf8mb4");
$mysqli->query("SET character_set_results=utf8mb4");
$mysqli->set_charset("utf8");
$sth = $mysqli->query($query);
if (!$sth) error_log("error Json query: $query");
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
//var_dump($rows);
print json_encode($rows, JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE);
?>
Yet each emoji occurrence is substituted by a variable number of question marks, yet
if I conversely execute the query of the php script directly on Sequel Pro, the emojis show correctly. Apparently there is therefore some error in the function above, but I tried anything, as you may see, without success.
How should the query be configured instead?
I thank Paul for the hint, the issue was related to the redundant use of:
$mysqli->set_charset("utf8")
that apparently replaced the previous commands, taking it out the emojis showed just fine.

Escaping single quotes and unicode symbols in php [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
How to prevent XSS with HTML/PHP?
(9 answers)
Closed 3 years ago.
I am using php to develop a news app where the admin will feed in the news item with its descriptions and finally get displayed to users. I am using mysql database.
Everything seems to be working fine but I am not able to escape single quotes as well as display certain currency symbols in my output text. I am using UTF-8 charset. I have used str_replace function but to no avail. I have also tried htmlentities($mystring, ENT_QUOTES | ENT_IGNORE, ‘UTF-8) and this actually ignored (removed) the single quotes and symbols instead of escaping and displaying them as they are. I am also using UTF-8_general_ci in the table column of mysql.
This is my code for retrieving the data :
try{
$pdo = new PDO("mysql:host=$hostname;dbname=$dbname",
$username,$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$stmt = $pdo->prepare("SELECT * FROM feed WHERE news_id = ?");
$stmt->bindParam(1,$myval);
$stmt->execute();
while($myrow = $stmt->fetch()){
$newarray[] = $myrow;
}
$mystring = $newarray[0]['body'];
$mystring = str_replace("'","/'",$mystring);
echo $mystring;
} catch(PDOException $e){
echo 'Failed to retriev database item ' . $e->getMessage();
}
Below is an example output:
International Monetary Fund�s (IMF�s) Extended Credit Facility (ECF) programme, the raising of the $3-billion Eurobond at a lower coupon rate, and the positive Fitch rating of Ghana should be enough to demonstrate that, the economy is on track to impact the strength of the Ghana cedi. �In the last couple of weeks, the Bank of Ghana for instance, released a large chunk of dollars to players in the banking industry to meet our requirements. That was unprecedented and we feel that should the speculations stop, the Ghana cedi will stabilise the way we want it�,
Try to specify utf8 for PDO connection
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $username,$password);
If you still got the issue, try to run this query straight after the connection
$pdo->exec("set names utf8");
These steps should fix your issue but only if the data is correctly saved on your database, and you have the correct charset collation specified for your db, table and fields.

PHP PDO -> SQL bad encode of czech language [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
im not new in php coding and have a problem. I need insert data to sql (czech language encoding).
I wanna input this string: +ěščřžýáíéůú
My DB output is: +ì¹èø¾ýáíéùú
So, im trying this solutions:
1) My config.php has this one:
header('Content-Type: text/html; charset=utf-8');
ini_set("default_charset", 'utf-8');
2) On top of the page is this meta-tags:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta charset="utf-8" />
3) When im connecting into db, im using:
#define("DB_Connect_Charset", "utf8");
$dsn = 'mysql:dbname='.DB_Connect_Database.';host='.DB_Connect_Hostname.';charset='.DB_Connect_Charset;
+
$this->pdo = new PDO($dsn, DB_Connect_Username, DB_Connect_Password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES ".DB_Connect_Charset));
Im trying some php functions like: iconv OR mb_convert_encode()
Database has utf8_czech_ci coding, when im trying to change it into windows-1251 or latin.. it doesnt help.
I spend a lot of time on stackoverflow and cannot find a solution about this problem, some problems will be deleted with SET NAMES UTF8.
Problem must be in part when im sending data to SQL, because if im put data manually into sql, im fetching it correctly.
Thank you for your time and help. Have a nice day!
When I'm binding parameters I use utf8_encode function for value. It was a mistake.

Encoding error PHP and Mysql for french [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 9 years ago.
I have a function that adds words in my database and for some reason it's putting them in all weird.
Here is my function :
function change_team($picname, $text){
include 'dbconnector.php';
$conn->query("UPDATE team SET tea_photo = '$picname', tea_text = $text WHERE tea_id = 1");
}
Then, I use a post form to put it in :
change_team($_POST['lala'], $_POST['text']);
$_POST['text'] = éÀéÀ works when I echo it before entering it into database, but in the database it does this: Éà É
I've tried everything, I've put my table as uft8_general_ci but it doesn't seem like it fixes anything.
In your dbconnector.php, do:
$conn->set_charset("utf8");
After connecting and selecting database. This will set the transmission encoding to UTF-8. (The table/column/db character set is just storage encoding).

Categories