This question already has answers here:
UTF-8 all the way through
(13 answers)
Closed 8 years ago.
I need to save text with language specific caracters in a mysql database.
I have setup the table and the field with utf8_general_ci
The test.php file to write into the database is also in utf8_general_ci:
<?php
include 'connect.php';
$x=$_GET["x"];
$d="INSERT INTO `test`
(`car`)
VALUES (' $x ')";
echo $d;
$resultins = mysql_query($d);
echo mysql_error();
echo "<br>".$id;
?>
In the browser I do:
/test.php?x=Vietnam_ờ_French_ç_German_ä
and the echo gives:
INSERT INTO test (car) VALUES (' Vietnam_á»_French_ç_German_ä ')
In database I get:
When I retrieve this data in another php and display it in the browserI get:
What am I missing?
Maybe before you start the select query, you should set the queries up to return UTF8 encoded string. First, use a <meta charset="utf-8"> in the head and before the query try this: mysqli_query($connection, "SET NAMES UTF8");. Hope it helps.
Place $dbc->set-charset('utf8') before any query.
Related
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.
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.
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I tried to create a script which will save all the IP address that came to my website (for adwords checking purposes). However when I try, the code doesnt save to database at all. Here is the code:
<?php
session_start();
// Create connection
$mysqli = new mysqli("localhost", "hashmicr_admineq", "monkeycool100", "hashmicr_sessionchecker");
date_default_timezone_set('Asia/Singapore');
$date = date("Y-m-d H:i:s");
$query = "INSERT INTO sessioncheck(ipaddress,date) VALUES (".$_SERVER['SERVER_ADDR'].", ".$date.")";
$mysqli->query($query);
/* close connection */
$mysqli->close();
?>
This is placed on the top of the PHP page.
Did I miss on any steps?
Need to add single quotes as both the field values contain non-numeric (other than 0-9) characters.
You can insert only numeric without single quotes.
Corrected SQL:
$query = "INSERT INTO sessioncheck(ipaddress,date)
VALUES ('".$_SERVER['SERVER_ADDR']."', '".$date."')";
This question already has answers here:
How can I prevent SQL injection in PHP?
(27 answers)
Closed 8 years ago.
I'm trying to change the content of a string (from user's input), I'd like to remove any character that will let my query fail. For example, if I insert a second name with a " ' " in it, the query will fail.
Since I have to then output these rows from the DB, I'm wondering if there's any way to insert the string in the database while replacing the special character with its HTML value so that when I'm outputting it, the browser will do the rest.
I'm leaving you an example:
$string = $_POST['user_input']; // Let it be Lol'd
$sql = "INSERT INTO table(field) VALUES('$string')";
Now without anything done to the string I'd get the query as:
INSERT INTO table(field) VALUES('Lol'd')
What I'm looking for is something to turn the ' into ' so that in the DB it's saved Lol'd but when I echo it it'll just print Lol'd
There are lot of solutions. You can use a function like htmlentities():
$string = htmlentities($_POST['user_input']); // Let it be Lol'd
$sql = "INSERT INTO table(field) VALUES('$string')";
To read the string from your MySQL table, use html_entity_decode()
try this
$string = str_replace("'","\'",$_POST['user_input']);
$sql = "INSERT INTO table(field) VALUES('$string')";
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).