I've such a problem , first take a look at mysql table =>
CREATE TABLE IF NOT EXISTS users(
id int(4) NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(40) CHARSET utf8 COLLATE 'utf8_unicode_ci' NOT NULL,
surname VARCHAR(40) CHARSET utf8 COLLATE 'utf8_unicode_ci' NOT NULL,
);
It is storing data successfully , but when I'm trying to retrieve this info to my .php file (which encoding is also utf8) it still showing me question marks (?????), why ? How can I solve it ?
UPDATE
Something I'm not doing well. So I've 2 php files, one is classA.php file in which I've defined class which is retrieving info from database and I've included this file (classA.php) into my default.php file where I want to see data.
I've exactly same table which is written above , and I'm writing
header('Content-Type: text/html; charset=UTF-8');
in the first line in default.php, but it still doesn't work, thanks for advices :))
SECOND UPDATE
This script I've in classA.php file , and its encoding is default like default.php file encoding. I just added in default.php file in first line this
header("Content-Type: text/html; charset=UTF-8");
but it still doesn't work.
Third update
sql =>
create table ok(
id int(2) not null auto_increment primary key,
name varchar(20) charset utf8 not null);
and php file
<?php
header('Content-Type: text/html; charset=UTF-8');
?>
<!DOCTYPE html>
<html>
<head>
<title>hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<?php
$con = mysqli_connect("host","user","pass","db");
if (mysqli_connect_errno()==0){
if ($r = mysqli_query($con,"SELECT * FROM ok")){
mysqli_set_charset($con,"utf8");
while ($d = mysqli_fetch_assoc($r)){
echo $d['name'] . "<br>";
}
}
}
if (isset($con)){
mysqli_close($con);
}
?>
</body>
</html>
I've inserted in ok table this=>
insert into ok(name) values("one"),("ერთი"),("two"),("ორი");
PS. special characters are Georgian :)
and it results English characters fine and Georgians with question marks :(
It doesn't work anyways :(
The fact that the PHP file is in UTF-8 doesn't necessarily mean that the data coming from/ going to the database is in UTF-8 too.
You didn't mention which extension you're using, but:
For mysql use mysql_set_charset($link,'utf8');
For mysqli use mysqli->set_charset('utf8') or the same as above with mysqli_
For PDO, when you connect include charset:utf8 in the DSN string.
Declare the HTML utf8 encoding:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
And/ Or transfer encodings in the headers:
header('Content-Type: text/html; charset=UTF-8');
try with
htmlentities($row['name'],ENT_QUOTES);
see htmlentities
Related
I have retrieve data from MySQL in php and post it to client:
if I use select * from users LIMIT 8;
no problem.. but when select * from users LIMIT 9; the last data retrieved broke the page.. when I debug in php I can see this data looks fine also:
1 = "CN=User1,OU=ARGE,OU=Personel,OU=Kullanicilar,OU=CompanyName,DC=company,DC=intra
2 ="CN=User2,OU=ARGE,OU=Personel,OU=Kullanicilar,OU=CompanyName,DC=company,DC=intra
3 ="CN=User3,OU=ARGE,OU=Personel,OU=Kullanicilar,OU=CompanyName,DC=company,DC=intra
4 ="CN=User4,OU=ARGE,OU=Personel,OU=Kullanicilar,OU=CompanyName,DC=company,DC=intra
5 ="CN=Öney,OU=ARGE,OU=Personel,OU=Kullanicilar,OU=CompanyName,DC=company,DC=intra
But there is no data returned from php.. its obvious that 'Ö' character cause this but I don't understand why its even looks true and my character encoding is:
My PageÖÖ
and this is also in top of my page :
header('Content-Type: text/html; charset=ISO-8859-1');
When I type a title '4 Tasks to completeÖÖŞŞŞ' in html of mypage.php it looks:
'4 Tasks to complete��???'
In stackoverflow it looks well, I want same for mine.. couldn't figure out the problem now in html or php or both of them?
EDITED: since I see in 'Öney' character in script variable I think in php page there is problem..
my connection settings:
$this->dbh = new PDO('mysql:host=localhost;dbname=webfilter;port=3306;connect_timeout=15', 'root', 'company');
$this->dbh->exec("set names utf8");
You're specifying the charset as UTF-8 in meta:
<meta charset="utf-8"/>
But you're specifying ISO-8859-1 in PHP:
header('Content-Type: text/html; charset=ISO-8859-1');
You'll need to have consistency, change the PHP header function to set the charset as UTF-8 too:
header('Content-Type: text/html; charset=utf-8');
Other steps that might help:
Setting the file encoding to UTF-8.
Setting the table columns to be in UTF-8 (utf8-general usually works).
Running the query SET NAMES utf8 after connecting to the database.
Change
header('Content-Type: text/html; charset=ISO-8859-1');
to
header('Content-Type: text/html; charset=utf-8');
In HTML, charset is utf-8, but it is different in PHP. Make sure that they are same.
Why when I'm reading a cyrillic text from database it's ok , but when I put this text in select-option menu I get strange symbols
http://prikachi.com/images/813/6589813g.jpg
http://prikachi.com/images/811/6589811I.jpg
I think that I put everywhere to be utf-8 but I don't know ...
in my html I use :
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
most likely you're not using the same charset (utf-8) everywhere so your data gets messed up at some point. depending on what exactly you're doing, you'll have to change/add one or more of the following points (maybe it's the SET CHARSET/mysql_set_charset you forgot):
tell MySQL to use utf-8. to do this, add this to your my.cnf:
collation_server = utf8_unicode_ci
character_set_server = utf8
before interacting with mysql, send this two querys:
SET NAMES 'utf8';
CHARSET 'utf8';
or, alternatively, let php do this after opening the connection:
mysql_set_charset('utf8', $conn); // when using the mysql_-functions
mysqli::set_charset('utf8') // when using mysqli
set UTF-8 as the default charset for your database
CREATE DATABASE `my_db` DEFAULT CHARACTER SET 'utf8';
do the same for tables:
CREATE TABLE `my_table` (
-- ...
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
assuming the client is a browser, serve your content as utf-8 and the the correct header:
header('Content-type: text/html; charset=utf-8');
to be really sure the browser understands, add a meta-tag:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
and, last but not least, tell the browser to submit forms using utf-8
<form accept-charset="utf-8" ...>
I have a database issue, that I'm unable to understand. I'm from Denmark and have made a sign-up system in PHP and MySQL. Now... I have made two tables seperately.
One of the tables (let's call it table1) displays my beloved danish letters (æøå) just fine, when I'm querying them from the database through PHP. But when I go to phpMyAdmin, then the letters are displayed wierdly... For instance: It looks like this in phpMyAdmin:
Bjørn (which is Bjørn)
But the again, when I get them from the database with a mysql_query('SELECT * FROM $tablename'), then it is displayed as 'Bjørn' (as it should).
Now to the problem...
In the other table (let's call it table 2), then in phpMyAdmin 'Bjørn' is displayed as 'Bjørn' (what seems correct). But when I pull it into PHP with mysql_query('SELECT * FROM $tablename') then it is displayed as 'Bj?rn'. All of the letters 'æøå' is displayed as a '?'.
I tried doing a SHOW TABLE STATUS, and it shows that the Collation is the same.
In table1, then the variables are VARCHAR(255), while in table2, the variables are TEXT.
Both tables are created like this:
CREATE TABLE >>tablename<< ( bla bla bla ) CHARSET=UTF8
You should connecto mysql like this
$link = mysql_connect('localhost', 'user', 'password');
mysql_set_charset('utf8',$link);
And then try executing the query it fetches properly
The problem here is , you should specify the charset while you are connecting to DB also .
Even while storing also your inserts gets garballed if you dont set the charset in your connection to utf8 so do verify once whether you are setting like this while connecting to DB or not .
Hope this helps
Also last but not least , while displaying in the browser also you should set html headers while dumping the data from DB to browser like below
<?php
header('Content-type: text/html; charset=utf-8');
?>
I don't know what's wrong with phpmyadmin, but in my own programming I do the following things:
PHP (before anything else):
header("Content-Type: text/html; charset=utf-8");
HTML:
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
DB query:
SET CHARACTER SET utf8;
Or if using PDO my dsn looks like:
mysql:dbname=mydb;host=localhost;charset=utf8
I have two .php files, a form with a text field where you put the names you want to search in the database, and a results file that processes the post...
The names in the database will be searched using this query:
SELECT * FROM acw_papers_web web
INNER JOIN acw_papers_web_autores aut
ON web.id_paper_web = aut.id_paper_web
WHERE aut.nombre_autor_pw LIKE '%autorname%'
ORDER BY web.probabilidad DESC
The problem is that when I send the post, insted of sending lópez, it sends lópez...
How can I fix it... both .php files are utf-8 encoded...
Since you did not provide much information, I cannot exactly pinpoint the problem. But here are two possible solutions:
Set the correct charset in the <head> of both files:
<meta charset="UTF-8" /> for HTML5 or <meta http-eqiv="Content-Type" content="text/html; charset=UTF-8" /> for everything else
Set the character set of the database tables to UTF-8. In MySQL:
ALTER TABLE acw_papers_web CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE acw_papers_web_autores CHARACTER SET utf8 COLLATE utf8_general_ci;
That's all I could think of, for now.
You need to use
urlencode() - URL-encodes string
urldecode() - URL-decode
Functions to send special characters in the Post Request.
I'm reading a UTF-8 encoded file using PHP and splatting the contents directly into a database. The problem is that when i encounter a character such as ” , it places the following †into the database.
How can i encode this correctly, i'm reading a UTF-8 file and my database column's collation is a UTF-8. What am i doing wrong? Is there a nice function i'm missing? Any help is welcome.
This is my table:
CREATE TABLE tblProductData (
intProductDataId int(10) unsigned NOT NULL AUTO_INCREMENT,
strProductName varchar(50) NOT NULL,
strProductDesc varchar(255) NOT NULL,
strProductCode varchar(10) NOT NULL,
dtmAdded datetime DEFAULT NULL,
dtmDiscontinued datetime DEFAULT NULL,
stmTimestamp timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (intProductDataId),
UNIQUE KEY (strProductCode)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE utf8_unicode_ci;
EDIT:
I'm reading the date like this:
$hFile = #fopen($FileName, "r") or exit("\nUnable to open file: " . $FileName);
if($hFile)
{
while(!feof($hFile))
{
$Line = fgets($hFile);
$this->Products[] = new Product($Line);
}
fclose($hFile);
}
use
mysql_query("SET NAMES utf8");
just after connection to DB and be sure that browser encoding is in utf-8, too
header("Content-Type: text/html; charset: utf-8");
You should set your connection encoding with this query
SET NAMES 'utf8'
before storing any data.
Keep also in mind that some database gui or web gui (i.e. phpMyAdmin) shows wrong encoding even if your data are encoded correctly. This happen for example with SequelPro on Mac and with phpMyAdmin in some environments.
You should trust your browser, i.e. show your inserted content in a page which has the
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
header and see if the data are shown correctly. Or even better trust mysql command line using the shell:
echo 'SELECT yourdata FROM your table' | mysql -uuser -pyourpwd db_name