PHP MySQL utf 8 encoding [duplicate] - php

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 12 months ago.
My trying to make an Ajax call to a PHP function that pulls out data from my database. I've run into a problem though.
My query looks like this
$query = "SELECT * FROM mytable WHERE field LIKE '%$string%'"
I then check on the number of rows returned by the query, but when i type in æ ø å then i my query returns 0 rows, although I know there are entries in the database that have æ ø å.. why is this

Set the connection to use UTF-8:
<?php
// MySQLi:
$connection = new MySQLi( /* ... credentials ...*/);
$connection->set_charset("utf8");
// MySQL:
$connection = mysql_connect(/* ... credentials ... */);
mysql_set_charset("utf8", $connection);
?>

in my case, I had to add this line:
mysqli_set_charset($con,"utf8mb4");

Related

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.

UTF-8 symbols not showing correctly on my website, SQL database, PHP code, utf8mb4_general_ci is set [duplicate]

This question already has answers here:
UTF-8 MySQL and Charset
(6 answers)
UTF-8 all the way through
(13 answers)
Closed 3 years ago.
I have imported 17 thousand lines of data into an SQL database via phpmyadmin running with Apache2 on Ubuntu Server 18.04.
When trying to display the data with PHP on my localhost website, UFT-8 symbols won't show properly.
I'm doing this:
$conn = mysqli_connect($servername, $username, $password, 'Drinks');
$sql = "SELECT * FROM drinks LIMIT 10";
$result = $conn->query($sql);
while($row = $result->fetch_assoc()) {
echo "<tr><th>" . $row["Name"]. "</th></tr>";
}
My database table is set to utf8mb4_general_ci and all rows are imported with INSERT.
My website head in my index.php is set to <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"> and i've also tried <meta charset="UTF-8">.
All symbols ARE showing correctly in phpmyadmin. Why won't they show on my website?
Thank you!
I did this to make it work:
$db->query('set character_set_client=utf8mb4');
$db->query('set character_set_connection=utf8mb4');
$db->query('set character_set_results=utf8mb4');
$db->query('set character_set_server=utf8mb4');
Thank you all.

Insert special characters into DB [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 7 years ago.
i'm tring to insert into my database the following string: "è" but each time i try in the database nothing is added. This is the code that i use to insert.
$string = mysql_real_escape_string($_REQUEST['string_passed']);
$query = "UPDATE my_table SET my_field = '$string' WHERE id = '$id'";
In my connection.inc.php file i have following code.
mysql_connect("localhost", "root", "") or die("Problem: ".mysql_error());
mysql_select_db("my_db") or die("Poblem: ".mysql_error());
mysql_query('SET NAMES utf8');
I know, i can insert $string using htmlentities but there is any other solution?
mysql_real_escape_string Escapes special characters in a string for use in an SQL statement.that is what you will see in its manual page. (by the way it is deprecated in php 5.5, use mysqli::real_escape_string)

MySQL & PHP special character issue [duplicate]

This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I am using MySQL 5.5 version
when i try to insert the ‘ special character in database it is automatically converted into ’ .
i changed the database character set to utf8 & character_set_connection to utf8 but i unable to get the expected result.
how to solve this issue ?
kindly help on this
You need to check how you are sending the data.
If the character set in the database is utf-8, you need to send like that to.
Try to encode the data before, like that:
$sql = "INSERT INTO tablex(field) VALUES('".utf8_encode($mydata)."')";
It is important to make sure that every part of your connection is using utf8, otherwise you will run into problems.
Below we will create a utf8 connection to the database, perform set names which is vitally important and then write using a utf8_encode method.
mysql_connect("host", "user", "pass");
mysql_query("SET character_set_results=utf8");
mysql_set_charset('utf8');
mb_internal_encoding('UTF-8');
mysql_select_db("my_db");
mysql_query("set names 'utf8'");
$sql = "INSERT INTO `table`(`foo`) VALUES('".utf8_encode($bar)."')";

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