Announcement

Collapse
No announcement yet.

Database class for PHP 5+ and MySQL (Help)

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Database class for PHP 5+ and MySQL (Help)

    Well I a total noice when it comes to OOP, so I was wondering if anyone could give me any tips, idea's or constructive critism.
    Code:
    <?php
    if (!defined('Access')) { die('Forbidden Access.'); }
    
    Class Database {
     protected $Connect, $ConnectStatus = false;
     public $TotalQueries, $Affected = null;
     
     public function __construct($Host, $User, $Pass, $Database) {
      $this->Connect = @mysql_connect($Host, $User, $Pass);
      if (!is_resource($this->Connect)) {
       $this->ConnectStatus = false;
       die('Cannot Connect To The MySQL Server.');
      } else if (!@mysql_select_db($Database, $this->Connect)) {
       mysql_close($this->Connect);
       $this->ConnectStatus = false;
       die('Cannot Select The Defined MySQL Database.');
      } else {
       $this->ConnectStatus = true;
      }
     }
     
     public function isConnected() {
      return $this->ConnectStatus;
     }
     
     public function doEscape($Var) {
      if (get_magic_quotes_gpc()) {
       $Var = stripslashes($Var);
      }
      return mysql_real_escape_string($Var, $this->Connect);
     }
     
     public function doQuery($Sql) {
      $Query = @mysql_query($Sql, $this->Connect);
      if ($Query === false) { 
       return $this->raiseError();
      }
      ++$this->TotalQueries;
      $this->Affected = @mysql_affected_rows($this->Connect);
      return $Query;
     }
     
     public function affectedRows() {
      return mysql_affected_rows($this->Connect);
     } 
     
     public function numRows($Sql){
      return mysql_num_rows($Sql);
     }
     
     public function insertId() {
      return mysql_insert_id($this->Connect);
     }
       
     public function doDisconnect() {
      $this->ConnectStatus = false;
      return mysql_close($this->Connect);
     } 
     
    }
    
    ?>

    My Twitter!

  • #2
    Re: Database class for PHP 5+ and MySQL (Help)

    What you could do with your..

    PHP Code:
     public function doEscape($Var) {
      if (
    get_magic_quotes_gpc()) {
       
    $Var stripslashes($Var);
      }
      return 
    mysql_real_escape_string($Var$this->Connect);
     } 
    .. function is build onto it and make it more reliable such as..

    PHP Code:
     public function doEscape($Var$Type 0) {
      return (
    $Type == 1) ? abs(intval($Var)) : mysql_real_escape_string($Var$this->Connect);
     } 
    For cleaning numbers:

    PHP Code:
    $db = new Database('localhost','username','password','database');
    $db->doEscape($inputTRUE); 
    for cleaning texts..
    PHP Code:
    $db = new Database('localhost','username','password','database');
    $db->doEscape($input); 
    or

    PHP Code:
    $db = new Database('localhost','username','password','database');
    $db->doEscape($inputFALSE); 

    Comment


    • #3
      Re: Database class for PHP 5+ and MySQL (Help)

      How ever, that is a valid point, but if you filter the data which in inputed, before entering it into the database, I would personally make sure it's a whole number :-)

      My Twitter!

      Comment


      • #4
        Re: Database class for PHP 5+ and MySQL (Help)

        Put doDisconnect into the __destruct function.
        [font='Verdana, Helvetica, sans-serif']ezRPG | pbbg.biz | Boring RPG[/font]

        Comment


        • #5
          Re: Database class for PHP 5+ and MySQL (Help)

          PHP Code:
          public function __destruct() {
           
          $this->ConnectStatus false;
           return 
          mysql_close($this->Connect);

          If I got it right? Should be, never used it though :-P

          My Twitter!

          Comment


          • #6
            Re: Database class for PHP 5+ and MySQL (Help)

            Yeah that's it Karlos.
            My personal opinion is that the close function should be it's own method.

            Database::close();

            And then the destructor should call Database::close();
            A minor detail, and it's my personal pref. What you have is theoretically sufficient since it's a good assumption that the connection would only be closed upon destroying the DB object, but it's not quite as thorough.

            -------------------------

            I noticed you've got your property declarations stacked:
            PHP Code:
             protected $Connect$ConnectStatus false;
             public 
            $TotalQueries$Affected null
            That's fine, and I used to do that as well (the Horizons Database class has it, in fact :P ), but I've since learned that in order to get code documentation and code completion in IDE's, those really need to be seperate lines.


            PHP Code:
                /**
                 * The MySQL connection resource
                 *
                 * @var resource
                 */
                
            protected $Connect;
                
                
            /**
                 * Are we currently connect?
                 *
                 * @var boolean
                 */
                
            protected $ConnectStatus false;
                
                
            /**
                 * How many queries have we done?
                 *
                 * @var integer
                 */
                
            public $TotalQueries 0;
                
                
            /**
                 * How many rows were affected by the last query.
                 *
                 * @var mixed
                 */
                
            public $Affected null

            Now, when you type in $this->Conne ---- you will see the doc block above Database::Connect in a pop up to remind you or others using your code of what Database::Connect is for.


            Depending on the IDE and how it handles this stuff, you can get code documentation for public references to the class as well.

            $db = new Database();

            $db->Con ---- and you will get code documentation here as well.

            If you do something weird like:

            $db = Database::getInstance();

            This would be the *singleton* instantiation method, you're getInstance doc block should have a:


            @return Database

            Which will tell the IDE that $db = Database::getInstance(); is creating and storing a Database object in $db.
            Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.

            Comment


            • #7
              Re: Database class for PHP 5+ and MySQL (Help)

              Your class is useless... it's just a wrapper for the normal PHP functions, and to use it, it would be harder than to use normal PHP function calls...

              Here are a few suggestions:
              - try not to use @, that's the slowest thing in PHP
              - instead of "die", throw an exception (do you really want to show that warning to the user for everyone using your class?)
              - split up your SELECT, UPDATE, INSERT, DELETE in different functions, that way, you can do more things depending on what query the user does (ex: count the number of selects vs updates)
              - use '__deconstruct", if you really want to kill the connection once your done (or store the class stats in the session, so the next refresh can use them)
              - you could probably make every function in there STATIC, depending on how you plan to use your class (ex: if you always use it with the same DB)
              - etc...

              hope that helps.

              Comment


              • #8
                Re: Database class for PHP 5+ and MySQL (Help)

                Originally posted by Vali
                you could probably make every function in there STATIC
                Doing that means that all the properties need to be public, and that means that you have no visibility control.
                Being able to use $this gives you visibility control.

                If you want to ensure that only one instance of a class is instantiated, the singleton pattern should be used instead of making everything in the class static.

                The standard php singleton pattern uses a getInstance() method, which gives access to the singleton in any scope, so you get the best of static class methods, but also the visibility control of non static classes.

                Hope that helps.
                Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.

                Comment


                • #9
                  Re: Database class for PHP 5+ and MySQL (Help)

                  Depends on what you need. And if you don't need $this, you should use STATIC methods, since they are about 4 times faster than normal ones.

                  Comment


                  • #10
                    Re: Database class for PHP 5+ and MySQL (Help)

                    Vali, you didn't reply at all to what I said about visibility...
                    Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.

                    Comment


                    • #11
                      Re: Database class for PHP 5+ and MySQL (Help)

                      There is nothing private or protected that you need for a DB class...

                      Comment


                      • #12
                        Re: Database class for PHP 5+ and MySQL (Help)

                        I'd definitely disagree with that. Just for a basic refutation of your argument Vali, let's consider a query counter.

                        You can have a static counter, but then anyone, anywhere, can set it anytime. I.e., the principle of encapsulation is broken. A query counter should only be set internally by the database class.


                        You may say this is trivial, but if the query counter can be blown out, then the db connection resource can be blown out! Without setting the connection resource as protected/private, you cannot be guaranteed that your connection won't be blown out.

                        Now, I respect that you personally don't see the value in this, so I'm only posting this response for the folks that may need to know this information.
                        Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.

                        Comment


                        • #13
                          Re: Database class for PHP 5+ and MySQL (Help)

                          Here you go:

                          Code:
                          <?php
                          /**
                           * This is your DTO that holds your counters (can be used by more than one class)
                           */
                          class myCounterDTO {
                          	public static $count = 0;
                          }
                          
                          /**
                           * This class does stuff
                           */
                          class myClass {
                          	public static function doStuff() {
                          		myCounterDTO::$count ++;
                          		echo "Doing stuff!\n";
                          	}
                          }
                          
                          /**
                           * This class does other stuff
                           */
                          class myOtherClass {
                          	public static function doOtherStuff() {
                          		myCounterDTO::$count ++;
                          		echo "Doing stuff!\n";
                          	}
                          }
                          
                          // Do some stuff and other stuff
                          myClass::doStuff ();
                          myOtherClass::doOtherStuff ();
                          myOtherClass::doOtherStuff ();
                          
                          // See how many times we did stuff
                          echo "Did stuff: " . myCounterDTO::$count . " times.\n";
                          ?>
                          Usually, 'myCounterDTO' should be a friend class of 'myClass' and 'myOtherClass', but PHP doesn't have that...

                          The idea is that you can have multiple classes that increase your 'query' count.

                          Comment


                          • #14
                            Re: Database class for PHP 5+ and MySQL (Help)

                            I'm sorry to say, but your "dto" is also lacking encapsulation.

                            Firstly, it should have a myCounterDTO::increment() method so that the counter variable doesn't have to be modified directly. Secondly, since this is a static class, with all members static, it should be using the singleton pattern. If you did that, you could hide the counter variable, and only allow access to the methods that operate on it.

                            increment()
                            and
                            get()
                            perhaps a
                            reset()

                            Cheers
                            Need a mod for your game? I do work that is second to none. PM me for further details or click on my banner to view my website.

                            Comment


                            • #15
                              Re: Database class for PHP 5+ and MySQL (Help)

                              The DTO class is just an object, that can be passed around. So you always know what your getting / setting.

                              As for the singleton, in this case, making it all static is WAY less code / faster to do, giving the same result.

                              Also, if all you have is one variable, it's just plain retarded to add 'increment()', 'get()' or 'reset()'...
                              I mean what's the point?

                              If you REALLY want to go out your way and waste your time, then I guess you can do it like that. It's only ~50 times slower and 20 times the code.

                              Comment

                              Working...
                              X