SitePoint Sponsor |
|
User Tag List
Results 51 to 54 of 54
Thread: character encoding issues
-
Aug 15, 2009, 15:19 #51
- Join Date
- Apr 2008
- Location
- europe://Bulgaria/Plovdiv
- Posts
- 116
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You can safely move the mysql_set_charset() call to the ConnectToDatabase() method, or better yet, create a new method for setting up the charset, and execute it upon initialization. Like:
Code PHP:public static $charset; public static function Initialize($varType, $varHost, $varPort, $varDatabase, $varUsername, $varPassword, $varCharset = 'utf8') { Error::Initialize(); if (!self::ValidDatabaseTypes($varType)) { Error::LogError("Database Type Invalid", "Database Type must be one of: " . self::DB_TYPES); } self::$host = $varHost; self::$port = $varPort; self::$type = strtolower($varType); self::$database = $varDatabase; self::$password = $varPassword; self::$username = $varUsername; self::$charset = $varCharset; self::$savedQueries = array(); self::$savedResults = array(); self::$connection = self::ConnectToDatabase(); self::SetTheCharset(); self::SelectTheDatabase(); } private static function SetTheCharset() { switch (self::$type) { case "mysql": @mysql_set_charset(self::charset,self::$connection) or Error::LogError("Charset setting", mysql_error(self::$connection)); break; } }
FYI, if you're trying to be DB agnostic, there's Zend Framework's Zend_Db, as well as PEAR's MDB2. Have you considered using one of those? Personally, I use Zend Framework, and am quite happy with it.XML_XSLT2Processor - perform XSLT 2.0 transformations in PHP.
(my library, all feedback welcome)
-
Aug 16, 2009, 08:25 #52
- Join Date
- Aug 2009
- Location
- Egypt
- Posts
- 64
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Merciiiiii
-
Aug 16, 2009, 22:00 #53
ok let's see how can we improve it and do a method for the charset alone cool that means I can use any where anytime in the code and I won't have to type it over and over...
Well what i did was specify the charset i as said before in the SelectTheDatabase private static method like
PHP Code:mysql_set_charset('utf8',self::$connection);
PHP Code:
// Select the Database
// Returns nothing
private static function SelectTheDatabase()
{
switch (self::$type)
{
case "mysql":
mysql_set_charset('utf8',self::$connection);
@mysql_select_db(self::$database, self::$connection) or Error::LogError("Database Selection", mysql_error(self::$connection));
break;
}
}
PHP Code:private static function SetTheCharset()
{
switch (self::$type)
{
case "mysql":
@mysql_set_charset(self::charset,self::$connection) or Error::LogError("Charset setting", mysql_error(self::$connection));
break;
}
}
PHP Code:public static $charset;
and then you have instantiated the method charset
PHP Code:self::SetTheCharset();
Thank you boen
Now I see you use
-
Aug 23, 2009, 06:43 #54
- Join Date
- Apr 2008
- Location
- europe://Bulgaria/Plovdiv
- Posts
- 116
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Sorry for the long time it took me to reply... I've been busy all week.
I use
PHP Code:public static $charset;
I have called the setTheCharset() method at the Initialize() method so that you don't have to always call it manually. If I don't call it, the charset won't be set to UTF-8, and you'll therefore use the MySQL default, which is latin1.
BTW, it's also worth to note that your classes have a very bad design... fundamentally so. The only reason I wrote the code in that way was so as to follow the pattern you're currently using... but that's far from what I'd consider using. Zend_Db is far closer to my ideal API (in fact, I currently can't really thing of anything in it that I'd do differently), which is why I use it.XML_XSLT2Processor - perform XSLT 2.0 transformations in PHP.
(my library, all feedback welcome)
Bookmarks