Jump to content

utf8


ricmetal

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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");
}

 

Link to comment
Share on other sites

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??

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.