Issue when inserting UTF8 characters in db from script - php

I have a small script that receive some data through GET and inserts data in a db. My problem is when sending some UTF-8 characters. The scritp receives them ok but inserts them in a weird way. I printed the query in my page, executed it with phpmyadmin and works ok that way. So, my problem is when executing the query through my script (it doesn't work if I execute a constant query with those characters). Does sending characters by post resolve the issue?
Thank you

Try this:
mysql_set_charset('utf8');

Your entire setup has to be UTF-8. That means your web page, PHP, the database connection, AND the database tables, all have to be in UTF-8 mode. Given it works in the admin pages and not via script, I'm guessing it's your database connection. Try doing a set names='utf-8' before doing your insert query, and see if that fixes things. If it does, then your db connection is using some OTHER character set and mangling your text as it goes from PHP->database.

Add this in the php file before your query:
mysql_query('SET NAMES utf8');
For example:
//DB connection
$con = mysql_connect('localhost', 'root', '');//db details
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydatabase", $con);//DB name
mysql_query('SET NAMES utf8');
//query
$sql="SELECT * FROM users WHERE uid=".$user_id;// your query
$result=mysql_query($sql);

Related

MySQL query working in phpmyadmin but not in php

I want to select data from a table in MySQL.
My code in php:
$conn = mysqli_connect($db_server, $db_benutzer, $db_passwort, $db_name);
$results= mysqli_query($conn, "SELECT * FROM `test` WHERE russia = 'привет'");
if(mysqli_num_rows($results) > 0) {
echo "Results";
}
else {
echo "No results";
}
mysqli_close($conn);
Here I'm getting "No results". But when I run the SELECT-code directly in phpmyadmin i get a result.
What's wrong?
Thank you
You have cyrillic characters in your query, so it may be necessary to set mySQL connection encoding. If you are using utf-8, insert following line after mysqli_connect:
mysqli_query($conn, "SET NAMES 'utf8'");
Or if your script is saved in windows-1251, use the following: mysqli_query($conn, "SET NAMES 'cp1251'");
For more information about connection character sets and encodings please see the manual
And why does the query work in phpMyAdmin? Because it probably sets encoding for you in the background.
You won't get 0 results in any way,you either get results or no results.
2.try removing the quotes from the table name
Check for any Encoding issues with the connection encoding and that the data on the value of column russia is parsed as something else.
Try executing the following query before executing your main query
mysqli_query($conn,"SET character_set_results='utf8',character_set_client='utf8',character_set_connection='utf8',character_set_database='utf8',character_set_server='utf8'");
The problems arise if there are Encoding issues in the connection.

can't get UTF-8 names into a mysql database

I'm having a problem getting UTF-8 names written into a MySQL database... Here's what I have.
PHP page head has....
<meta charset="utf-8">
the MySQL column is: Char (80) with utf8_unicode_ci (these were originally latin1... I've changed them to UTF-8, truncated the database, then rerun the code)
The variable echoes to screen: Germán Mera
but writes it to database as Germán Mera
I tried putting utf8_encode(); around the variable, but then it writes to database as: Germán Mera and screen as Germán Mera (I know that command only works on iso-8859-1.. I think the JSON page is already UTF-8)
Here is an excerpt of the code I am using to get the name (for sake of simplicity, I'm only showing relevant code - I know what's shown below is not secure)
$str = file_get_contents('http://fantasy.mlssoccer.com/web/api/elements/647/');
$jsonarray = json_decode($str, true);
$name = $jsonarray['web_name'];
mysqli_query ($con, "INSERT INTO mlsprices (name) VALUES ('$name')");
Any idea how I can get this to write to the database properly? When I search, I only get quite complicated answers (eg, this) and there's surely an easier way.
Try using SET NAMES 'UTF8' after connecting to MySQL:
$con=mysqli_connect("host", "user", "pw", "db");
if (!$con)
{
die('Failed to connect to mySQL: ' .mysqli_connect_errno());
}
/* change character set to utf8 */
if (!$con->set_charset("utf8")) {
printf("Error loading character set utf8: %s\n", $con->error);
}
As the manual says:
SET NAMES indicates what character set the client will use to send SQL
statements to the server... It also specifies the character set that the server should
use for sending results back to the client.

how to correct font error of mysql

hello i am using MYSQL for my data base. When i use Mongolian font. it saves in MYSQL like ????. How to correct this?
When i save in English it looks ok but in Mongolian it looks like ??? in the picture.
In the screen it also looks like ????
my MYSQL insert code is:
$sql ="INSERT INTO core_network (system_name,sub_cat,location,alarmtype,severity,start_time,end_time,reason,shift_operation)
VALUES ('$S_name','$subcategory','$city','$a_type','$severity','$S_time','$F_time','$reason','$shift_operation')";
how to correct?
$connection = mysql_connect($host1, $user, $pass)
or die("Could not connect: ".mysql_error());
mysql_select_db($database1,$connection)
or die("Error in selecting the database:".mysql_error());
after connect to DB and before use "SELECT" use this code :
mysql_query("SET CHARACTER SET utf8",$connection);
You need to set the collation of the columns to an UTF-8 one like utf8_general_ci.
Also you have to make sure that the connection charset is UTF-8 as well, for example by issuing
SET NAMES "utf8"
before any other SQL.

ÆØÅ doesn't display correctly in MySQL

I have set up a table in phpMyAdmin. I haven't changed the charsets or anything. I inserted a text in a new row, and when I try to SELECT that row and output it with PHP, the letters ÆØÅ are displayed as ���, however if I try to edit the field in phpMyAdmin, the letters are displayed correctly. What do I do wrong that phpMyAdmin does correctly?
If your PHP file is already UTF-8 encoded, you should tell your database, that you need UTF-8. Instead of fiddling with the configurations of MySQL, just tell your connection object, which character-set you expect, the database does the rest for you.
This is an example for a mysqli connection object:
$db = new mysqli($dbHost, $dbUser, $dbPw, $dbName);
$db->set_charset("utf8");
Afterwards your queries will return UTF-8 encoded results.
SET NAMES 'charset_name'
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
First you will want to determine if it is the browser charset that is wrong, or mysql.
Try swapping the charset in your browser to utf8 or if it is already to iso-8859.
If that doesn't fix it try changing the charset in your query by doing
SET CHARACTER SET charsetname;

How can I get UTF8 to work correctly using mysql and php?

I have a database where the table is encoded as utf8. I have a value in it that's in Korean. The characters display fine in the database. But when they are echoed from the database I get a bunch of question marks.
Here is my code, after the connect and select_db statements:
mysql_query('SET NAMES utf8');
$query = 'SELECT * FROM english WHERE id = ' . $_GET['dealerID'];
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_array($result);
What am I doing wrong here? (Yes, 'english' is the right table). I tried Michael's suggestion below to encode the table as utf8_general_ci and I get a MySQL error. Suggestions? What's the correct name of the character set?
If I run a query in PHPMyAdmin, I get 서울시 서초구 서초1동 1425-10 세중프라자 4층/
Review this guide which talks about UTF8, mysql, and PHP all together.
Summary:
make sure the browser knows the page is in utf8
<?php header("Content-type: text/html; charset=utf-8"); ?>
The table in mysql needs to be set for utf8 as well as the string fields within the db
utf8_general_ci
tell php that when it talks to mysql to talk in utf8
mysqli_set_charset('utf8');
Here are some more details as well.

Categories