Query special characters from mysql db - php

I am using PHP 7.1.8 and I am trying to query my db. However, the serialized arrays have special characters, which are shown as the following: �
Find below an example of my code:
// connect to db
$dbname = $conf['dbName'];
$dbuser = $conf['user'];
$dbpass = $conf['pwd'];
$dbhost = $conf['host'];
// Create connection
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
$arr = $conn->query("SELECT * FROM postmeta WHERE post_id = "100" and meta_key = 'val' LIMIT 1;")->fetch_assoc()["meta_value"];
When querying the database directly, I get the correct value for �. See below my ``$arr`:
This error results basically in that I cannot unserialize the data correctly.
Any suggestions how to fix this error?
I appreciate your replies!

The problem lies in collation. Your MySQL returns specific symbols (temperature symbol) that is not supported by your current encoding.
Try to execute this query right before your selects:
set names 'utf8';
Also, if you're showing this data in some kind HTML response, don't forget to add charset=UTF-8 to your meta tag of Content-type.

$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
/* change character set to utf8 */
if (!$conn->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $conn->error);
exit();
} else {
printf("Current character set: %s\n", $conn->character_set_name());
}
$arr = $conn->query("SELECT * FROM postmeta WHERE post_id = "100" and meta_key = 'val' LIMIT 1;")->fetch_assoc()["meta_value"];
Please check the output.

Related

Wrong character encoding

I have two forms on two different pages which are used to insert data to an MySQL database. I have some special character like 'čšžćđ' in my form data which I pass via the forms to the insertion scripts.
The data from the first form gets inserted correctly, while some fields from the second form contain the '?' characters, which would indicate a mismatch in encoding.
The two insertion scripts of both the forms are using the same file to connect to the database and set the encoding, like below:
<?php
$username = "root";
$password = "";
$servername = "localhost";
$conn = mysqli_connect($servername, $username, $password);
mysqli_select_db($conn, "testdb");
if (!$conn) { // check if connected
die("Connection failed: " . mysqli_connect_error());
exit();
}else{
/* change character set to utf8 */
if (!mysqli_set_charset($conn, "utf8")) {
// printf("Error loading character set utf8: %s\n", mysqli_error($conn));
} else {
// printf("Current character set: %s\n", mysqli_character_set_name($conn));
}
mysqli_select_db($conn, "testdb");
//echo "Connected successfully.";
// Check if the correct db is selected
if ($result = mysqli_query($conn, "SELECT DATABASE()")) {
$row = mysqli_fetch_row($result);
//printf("\nDefault database is %s.\n", $row[0]);
mysqli_free_result($result);
}
}
?>
I guess this would mean, that the client character encoding isn't set correctly? All database tables have the utf_8 encoding set.
Try to set encoding on top of the page
<?php
header('Content-Type: text/html; charset=utf-8');
other code...
Are you talking about HTML forms? If so,
<form accept-charset="UTF-8">
Is it one ? per accented character? When trying to use utf8/utf8mb4, if you see Question Marks (regular ones, not black diamonds),
The bytes to be stored are not encoded as utf8. Fix this.
The column in the database is CHARACTER SET utf8 (or utf8mb4). Fix this.
Also, check that the connection during reading is utf8.
The data was probably converted to ?, hence cannot be recovered from the text.
SELECT col, HEX(col) FROM ... to see what got stored.
? is 3F in hex.
Accented European letters will have two hex bytes per character. That includes each of čšžćđ.
Chinese, Japanese, and Korean will (mostly) have three hex bytes per character.
Four hex characters would indicate "double encoding".

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.

Nesting MySQLI Queries

I am trying to pull a number from one table inside a database, and then use that number to process a query on another table in the same database.
The code doesn't spit out any errors - it just doesn't return a string! I am trying to understand mysqli and the whole array structure, but I'm having difficulty figuring out why this isn't working. I believe I am trying to successfully turned the original array into a string for use in the second query, which I also translate into a string for the echo. It's just that for some reason it's not printing anything! If I take out the nested loop then it prints the active_event number just fine. I'm at a loss!
<?php
$DBServer = 'localhost';
$DBUser = 'user';
$DBPass = 'pass';
$DBName = 'database';
$conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($conn->connect_error) {
trigger_error('Database connection failed: ' . $conn->connect_error, E_USER_ERROR);
}
$get_active_event = mysqli_query($conn, "SELECT active_event FROM asp_config");
while($active_event = #mysql_fetch_assoc($get_active_event)){
$get_event_name = mysqli_query($conn, "SELECT * FROM asp_events WHERE id = {$active_event['active_event']}"); echo $get_event_name->fetch_object()->event_name;}
$conn->close();
?>
Thanks!
-Philip
I suggest to change the logic of your piece of code modifying you db schema in a more efficient way.
I'd fetch the results in a single query joining the two tables asp_config and asp_events or, even better, if possible get rid of asp_config and add a column is_activeor something like this to asp_events table.
Then you just have to cycle with while-loop without the second query because all you need to know is in the first results set.
Be careful to use the error suppression (#) you need to know if there is an error and handle it. Suppress without knowing it's a bad pratice
Unfortunately joining the two tables isn't an option, and I have other queries that need to use the same type of functionality so merging all of the tables into one just isn't doable. That all said, I did figure it out. I think the biggest issue was that I wasn't exiting out of the SQL mode before trying to insert the PHP variable, so I ended up querying a null row which returned a blank dataset. The final code I used is:
<?php
$DBServer = 'localhost';
$DBUser = 'user';
$DBPass = 'pass';
$DBName = 'actionsports';
$con = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
if ($con->connect_error) {
trigger_error('Database connection failed: ' . $con->connect_error, E_USER_ERROR);
}
$get_active_event = mysqli_query($con,"SELECT * FROM asp_config");
while($active_event = mysqli_fetch_array($get_active_event))
{
$get_event_name = mysqli_query($con, "SELECT * FROM asp_events WHERE id=('" .$active_event['active_event'] ."')");
if ($get_active_event === false) {
exit("Error: " . mysqli_error($con));
}
while($event_name = mysqli_fetch_array($get_event_name))
{ echo $event_name['event_name'] ;}}
$con->close();
?>
In this case I do have a query loop inside another loop, and it does return the correct data. It might not be the prettiest code, but it works and is what is required for my situation.
Thanks for the help!

Characters not displaying correctly on a UTF-8 website

I've done everything I can think of, but special characters are not displaying correctly on this webpage.
For example, in the database it's:
But on the site it's:
Nouveaux R�alistes
Here's everything I've checked...
The database is set to UTF-8:
The page was written in NetBeans, with the document encoding set to UTF-8:
The page header declares UTF-8:
The meta charset is set to UTF-8:
I've even added the following line to my .htacess:
But there characters are still not displaying correctly, and I get the following error from the W3C Validator:
I feel like I've attempted everything, but it still won't work. (I've even tried htmlspecialchars and htmlentities in PHP, but the page doesn't even render!)
UPDATE
As requested, here is the code I'm using:
class Exhibition {
public $exhibitionDetails;
public function __construct(Database $db, $exhibitionID){
$this->_db = $db;
$params['ExhibitionID'] = $exhibitionID;
$STH = $this->_db->prepare("SELECT *
FROM Exhibition
INNER JOIN Schedule
ON Exhibition.ExhibitionID = Schedule.ExhibitionID
WHERE Schedule.Visible = 1
AND Exhibition.ExhibitionID = :ExhibitionID;");
$STH->execute($params);
$this->exhibitionDetails = $STH->fetchAll(PDO::FETCH_ASSOC);
}
}
And...
try {
$db = new Database(SITE_ROOT."/../");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$exhibition = new Exhibition($db,$_GET['id']);
} catch (PDOException $e) {
echo "<p class='error'>ERROR: ".$e->getMessage()."</p>";
}
And finally...
<p><?php echo $exhibition->exhibitionDetails[0]["Desc"]; ?></p>
If you are using mysql_* functions:
mysql_query("SET NAMES 'utf8'");
If you are using PDO
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';
$options = array(
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
);
$dbh = new PDO($dsn, $username, $password, $options);
It sets connection encoding.
It's been a few years since I've used PHP but back then it didn't natively support Unicode and a quick search of google tells me it still doesn't. You can still make it work though.
Here's a great link:
Encodings And Character Sets To Work With Text

Polish characters in mysql response

I have a problem with polish characters. I can't get correctly written words, like '?ukasz' instead of "Łukasz" or even "null", when it supposed to be "Kraków". I tried "mysql_set_charset('utf-8'/'iso-8859-1')" after mysql_connect or iconv(on json_encode($output)) and it's still the same (except now there is "Krak\u00f3" instead of "null"). I'll appreciate any help.
This is a php file for my Android app:
$id_client = $_REQUEST['id_klienta'];
$con=mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('courier_helper') or die(mysql_error());
$sql=mysql_query("SELECT * FROM `clients` WHERE id_klienta='$id_client'");
while($r=mysql_fetch_assoc($sql))
$output[]=$r;
print(json_encode($output));
mysql_close($con);
?>
You have to make sure, that you are using UTF-8 everywhere:
script file encoding (UTF-8 instead of ANSI) - you can set encoding it in Notepad++
html code (meta charset)
database table charset (when you are creating table or database)
database mysql_set_charset('utf8', $connection_obj);
database SET NAMES utf8 - run that SQL command after connecting
And one more thing - get familiar with PDO. This is my PDO connect function I use:
function DbConnect()
{
$db_host = "localhost";
$db_name = "database_name";
$db_user = "your_username";
$db_pass = "your_passwd";
$link = new PDO("mysql:host=$db_host;dbname=$db_name; charset=UTF-8", $db_user, $db_pass);
$link->exec("set names utf8;");
return $link;
}
You can use that function like this (this is PDO example):
$link = DbConnect();
$query = $link->prepare("SELECT id FROM wp_users");
$query->execute();
$result = $query->fetchAll(PDO::FETCH_ASSOC);
You should have your database storing data as UTF8, which means converting your existing tables.
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
You also need to make sure your connection to the database is UTF8. You can make sure of that by running a SET NAMES query right after your connect.
SET NAMES UTF8
As others mentioned, you should start using PDO.

Categories