Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Charset #78

Open
marcosfreitas opened this issue Apr 15, 2016 · 1 comment
Open

Wrong Charset #78

marcosfreitas opened this issue Apr 15, 2016 · 1 comment

Comments

@marcosfreitas
Copy link

marcosfreitas commented Apr 15, 2016

I'm using charset latin1 on my database and my pages have iso-8859-1 codification and I'm including this code on all pages to force it:

header('Content-type: text/html; charset=ISO-8859-1');

But when I try to insert string with special characters the query fails with this report:

charset

This is my query:

// the parameters for the query bellow
$params = array (
//"fk_user" => $this->fk_user,
"fk_user" => 1,
"ad_title" => $this->ad_title,
"ad_description" => $this->ad_description,
"ad_files" => "image1.jpg,image2.jpg,image3.jpg",
"ad_type" => (int) $this->ad_type,
"ad_mode" => (int) $this->ad_mode,
"ad_reward" => (float) $this->ad_reward,
"ad_location" => $this->ad_location,
//dates inside the query..,
"ad_status" => $this->ad_status,
"ad_slug" => $this->ad_slug,
"ad_tags" => $this->ad_tags,
);
// the query
$sql = 'REPLACE INTO ads (';
$sql .= 'fk_user, ad_title, ad_description, ad_files, ad_type, ad_mode, ad_reward, ad_location, ad_cdate, ad_udate, ad_status, ad_slug, ad_tags, ad_solved, ad_reports';
$sql .= ') VALUES (';
$sql .= ':fk_user, :ad_title, :ad_description, :ad_files, :ad_type, :ad_mode, :ad_reward, :ad_location, NOW(), NULL, :ad_status, :ad_slug, :ad_tags, 1, 0';
$sql .= ')';
// execute the query
$add = $db->query($sql, $params);

@marcosfreitas
Copy link
Author

I can fix using utf8_encode() on all strings that have special characters inside $params array. But why? lol

"ad_title" => utf8_encode($this->ad_title)

See bellow that those two last lines have correct charset. They has been inserted with REPLACE INTO (see https://github.com/indieteq/indieteq-php-my-sql-pdo-database-class/issues/77) and using utf8_encode()

charset fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant