why am i getting this £10 using charset=utf8 - php

If i enter a £ (UK Pound Symbol) into my DB why does it always add to the database with as £.
I thought this was down to the charset but thought utf8 should cure this
EDIT i am setting the charset as below
$db = new PDO('mysql:host='.DBHOST.';dbname='.DBNAME.';charset=utf8', DBUSER, DBPASS);
any advice please.

Use this before any other query
SET NAMES utf8;

Related

How to use UTF-8 with Firebird and PHP Data Objects (PDO)

With mysql I can just do this to solve the problem:
$diveOptions = array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"); // set names utf-8 and works well
$pdo = new PDO('mysql:host=mysql.example.com;dbname=example_db',"username","password",diveOptions);
But I can't do the same with the firebird's pdo connection.
How can I solve this problem of UTF-8?
Already tried charset=utf8 in pdo configuration don t work too..
$str_conn = "firebird:dbname=C:\db\banco.gdb;charset=utf8;host=localhost";
$dbh = new PDO($str_conn, "SYSDBA", "masterkey");
In my db was set to nome like this:
DEFAULT CHARACTER SET NONE COLLATION NONE;

PHP & MySQL - Problems with UTF-8 encode

I have a form with a input that accept UTF-8 characters:
<form method="post" action="faz-personagem.php" accept-charset="UTF-8">
<div class="row">
<label for="nome">Nome</label>
<input type="text" name="nome">
</div>
...
<button type="submit">Enviar</button>
</form>
And a script that send the data to a database:
<?php
header("Content-Type: text/html;charset=UTF-8");
$conexao = mysql_connect('localhost', 'root', 'pass');
mysql_select_db('pan-tactics');
$nome = $_POST['nome'];
$nome = utf8_encode($nome);
$sql = "INSERT INTO personagens VALUES";
$sql .= "('$nome')";
$resultado = mysql_query($sql);
echo 'Personagem criado com sucesso.';
mysql_close($conexao);
?>
I also have specified in the creation of the database the collation utf8_unicode_ci and yet what I get is wrong special characters:
What can I do to fix it?
Keep in mind that collation is not the same as charset. You need to set your database and table to be in UTF-8 encoding. This can be done with running this SQL command (only need to be done once).
ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Furthermore, you should set the mysql_* connection to UFT-8. This code should be placed directly after connecting to your database.
mysql_set_charset("utf8");
I see you already set the PHP header to UTF-8 as well, so that's good.
Keep in mind that usage of mysql_* functions are deprecated and no longer maintained; you should switch to PDO or MySQLi for security reasons.
If neither of these steps helped you, you may need to save the document itself as UTF-8 w/o BOM, this can be done in Notepad++ as Format -> Convert to UFT-8 w/o BOM.
i had the same problem.
This was my solution:
$con = mysqli_connect('localhost', 'secretuser', 'secret', 'mydb');
mysqli_set_charset($con, "utf8mb4");
in your case:
$conexao = mysql_connect('localhost', 'root', 'pass');
mysqli_set_charset($conexao, "utf8mb4");
Maybe this will help someone who comes across this old post.
Set your connection with the database to utf-8:
mysql_set_charsethttp://php.net/manual/en/function.mysql-set-charset.php
U should use mysqli_ functions or PDP rather then mysql_ functions!
Check your collation of your database (including columns and tables).
Also, try adding mysql_query("set names 'utf8'"); after the connection to database in your code.
Were you expecting João for the first name? If so, you have "Mojibake".
João is even worse -- It looks like you Mojibaked João to get João, then Mojibaked that to get João.

Character issues when inserting into MySQL DB

Having an issue with strange characters showing up when inserting into a database, have tried tirelessly to figure out the issue but I am out of ideas...
Basically if I insert this data like so (this is just testing):
$valy = "…industry's favorite </em><strong><em>party of the year</em></strong><em>, </em><a href='http://www.unitingagainstlungcancer.org/getinvolved/strolling-supper-with-blues-news'><span class='s1'><em>Joan's…";
$valy = mysql_real_escape_string($valy);
$query = "INSERT INTO test_table (data) VALUES ('".$valy."')";
mysql_query($query,$dbhandle);
this will end up in the database (notice the A characters):
"...industry's favorite party of the year, http://www.unitingagainstlungcancer.org/getinvolved/strolling-supper-with-blues-news'>Joan's..."
I have tried to line up all the character settings:
php default_charset = utf-8
mysql table & row charset = utf-8
Mysql instance variables:
character set client utf8
(Global value) latin1
character set connection utf8
(Global value) latin1
character set database latin1
character set filesystem binary
character set results utf8
(Global value) latin1
character set server latin1
character set system utf8
What could this issue be?
One thing you may be missing is when you setup the connection. There you should also set the encoding to utf8.
Example:
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
However, don't use the mysql extension, it's deprecated: http://php.net/manual/en/function.mysql-set-charset.php
Try to set the mysql connection to UTF-8 as well:
mysql_query("SET NAMES 'utf8'");

How to properly display non-redable characters stored in a MySQL Database

I have São Tomé and Príncipe stored in a MySQL database.
It displays on my web page as S�o Tom� and Pr�ncipe
Is there anyway to display it properly straight from the database?
My MySQL Type is MyISAM and the collation is utf8_general_ci
My web page charset is <meta charset="utf-8">
Field type is set to varchar(30) and field collations is same as table utf8_general_ci
PHP FUNCTION TESTS
utf8_decode() converted characters to S?o Tom? and Pr?ncipe
htmlspecialchars_decode() converted characters to S�o Tom� and Pr�ncipe
html_entity_decode() converted characters to S�o Tom� and Pr�ncipe
quoted_printable_decode() converted characters to S�o Tom� and Pr�ncipe
htmlentities() returned blank result
GOT IT TO WORK THANKS TO #SAEVEN
Ran this right before my query SELECT
mysqli_query($GLOBALS['db_link'], "SET NAMES 'utf8'");
Could it be that you need to specify your namespace collation at the connection level?
I assume that you've covered the basics, you've got the charset as utf8 in your HTML markup.
Try executing this SQL statement before you do any of your database queries:
'SET NAMES utf8'
e.g., if you are using PDO
// do your regular connection logic whatever it is
$dbc = new PDO( .... );
$dbc->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbc->query( 'SET NAMES utf8' );
// run your queries beneath
....
or can use the shorthand
new PDO(
'mysql:host=mysql.example.com;dbname=awesome_db',
"username",
"password",
array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")
);
Whatever DBAL you use, that query'll work. Just run it on the same connection you use to pull your data, before pulling it (only needs to be done once)

– like symbols when using text editor

When I used text editors to insert text into database, symbols like - , ' etc get converted to – like symbols. How can I avoid these symbols?
Set names to UTF8 before inserting into your database.
SET NAMES utf8
It sounds like your connection is using latin1 encoding. I struggled with the same thing for months.
http://forums.mysql.com/read.php?103,46870,47245
You need to set encoding to utf8 using method mysql_set_charset('utf8',$link);
<?php
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
$db_selected = mysql_select_db('your_db_name', $link);
?>
And then the usual stuff you do with your connection
Check the encoding (collation) used on the database. Try changing it to utf8_general_ci or the equivalent choice on the Database you are using.

Categories