SitePoint Sponsor

User Tag List

Page 3 of 3 FirstFirst 123
Results 51 to 54 of 54
  1. #51
    SitePoint Zealot boen_robot's Avatar
    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)

  2. #52
    SitePoint Enthusiast SmAsH_HaCkEr's Avatar
    Join Date
    Aug 2009
    Location
    Egypt
    Posts
    64
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Merciiiiii

  3. #53
    SitePoint Wizard co.ador's Avatar
    Join Date
    Apr 2009
    Posts
    1,054
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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); 
    Entire SelectTheDatabase method below together with the charset specification in it.
    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::$databaseself::$connection) or Error::LogError("Database Selection"mysql_error(self::$connection));
                break;
            }
          } 
    better yet in case I need to define a charset in any other database there will now a method for it the one you created above and displayed again below..

    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;
            }
          } 
    I have noticed that you used this:

    PHP Code:
    public static $charset
    Above the Initialize method why?

    and then you have instantiated the method charset
    PHP Code:
    self::SetTheCharset(); 
    inside the Initialize method why have you done this two things?

    Thank you boen


    Now I see you use

  4. #54
    SitePoint Zealot boen_robot's Avatar
    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
    to define a public static property, used to store the current charset. You should do this in order to preserve data across functions of the same class. PHP can dynamically create properties, so this is not a must in it, but it's a good habbit to create non the less.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •