ricmetal Posted May 20, 2010 Share Posted May 20, 2010 hi guys im getting alot of crap characters where specials characters like é should be, in mysql. not "yet, another encoding post", i've done a few things to try and get the right encodings but i havent figured it out. any extra help is appretiated. here's what i've done. my phps are utf-8 encoded and the meta is declared before any php code to send data to the server. <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> i created the database and tables with utf8 encoding, plus i altered the database right before creating the tables. i manually created the database with all utf-8 encoding options i could find. then altered it. ALTER DATABASE db_name CHARACTER SET utf8 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT COLLATE utf8_general_ci ; created the tables in sql: CREATE TABLE `tableName` ( `user_email` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL, `id` int(50) NOT NULL DEFAULT '0', PRIMARY KEY (`user_email`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE utf8_general_ci; more: iconv (whatever that is) on my server is set to ISO-something so far the only way i can convert the encoding and make the special characters appear in the database correctly is by writing mysqli->set_charset("utf8") after each mysqli connection but i wanted to set it up so i dont have to make that encode thing. please any help? Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/ Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 mysqli->set_charset("utf8") Unfortunately that's the right thing to do. You can define default connection charset in my.ini file if you have access to it, but if you ever change your host to one that does not allow for that you're duped. You can also extend MySQLi to choose encoding for you. class MyMySQLi extends MySQLi { __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) { parent::__construct($host, $username, $passwd, $dbname, $port, $socket); $this->set_charset("utf8"); } } But the best thing you can do, is to layout your application in such a way, that a connection is established in one place only, so this one extra line for setting up encoding is not a bother. Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061269 Share on other sites More sharing options...
ricmetal Posted May 20, 2010 Author Share Posted May 20, 2010 cool if im lucky my webhost can change these ini settings for me and i avoid having an extra line of code on too many php files! thanks for the replies mchl! Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061280 Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 As I said, the best option is to have database connection in one file only. Your choice Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061310 Share on other sites More sharing options...
ricmetal Posted May 20, 2010 Author Share Posted May 20, 2010 i thought configuring in one place sounded better --- anyway, not so lucky, my host said "a no no" so, back to writing code i have the connection in a single php and again it sounds better to extend mysqli but i never did that, and you have a guru tag so you must know one thing or two. so ill go with what u r saying. but i am looking at how i set up the code include '../phpincludes/dbCon.php'; if (mysqli_connect_errno()) { include '../phpincludes/errorConnecting.php'; }else{ if($mysqli->set_charset("utf8")) { if($stmt = $mysqli->prepare("SELECT... --code body... how can i go about implementing the if charset statement in the single php file? i dont's see how i can, except including at the top and bottom of the code body. Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061348 Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 What do you have in dbCon.php ? Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061359 Share on other sites More sharing options...
ricmetal Posted May 20, 2010 Author Share Posted May 20, 2010 i've only got the mysqli connection... $mysqli = new mysqli(); Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061376 Share on other sites More sharing options...
Mchl Posted May 20, 2010 Share Posted May 20, 2010 Why don't you move some more code there? if(!isset($mysqli)) { $mysqli = new mysqli(); if (mysqli_connect_errno()) { include '../phpincludes/errorConnecting.php'; die(); } $mysqli->set_charset("utf8"); } Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061382 Share on other sites More sharing options...
ricmetal Posted May 20, 2010 Author Share Posted May 20, 2010 i may be looking at things wrong but this is the format in which i have most of my code <?php $connect = "no"; if($connect == "no") { die();//i dont use die(), just in here to make the point of why i dont have much things in the one single php file, nor do i use the die() } echo "this does not trace and i still want it to trace. also the html isn't parsed"; ?> <html> <head> </head> <body> hi </body> </html> so how do i set up the code to make it work like the way you set it up?? Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061399 Share on other sites More sharing options...
ricmetal Posted May 21, 2010 Author Share Posted May 21, 2010 i figured it out. kind of hacky but got it working using a success variable, instead of the die() function thanks mchl Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061735 Share on other sites More sharing options...
Mchl Posted May 21, 2010 Share Posted May 21, 2010 Yeah, I assumed that whatever there is in ../phpincludes/errorConnecting.php file is diaplying an error message and the script execution should end after that. Quote Link to comment https://forums.phpfreaks.com/topic/202413-utf8/#findComment-1061741 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.