PHP Tutorial: Data Management Class

Loading...

Sign in or sign up now!
Alert icon
Upgrade to the latest Flash Player for improved playback performance. Upgrade now or more info.
7,317
Loading...
Alert icon
Sign in or sign up now!
Alert icon

Uploaded by on Feb 18, 2010

Create a data management class that will allow you to handle table data efficiently across all your php scripts. This is a very common and powerful technique. I will be uploading more tutorials so subscribe to my channel. Thanks.

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (phpclass)

  • Nice tutorial, one thing though: why are you using static methods?

    Because, isn't calling self::_dbConnect() at every method going to use up a lot of resources, especially if the MySQL server is remote?

    What I personally would do, is create a private attribute ($mysql_link for instance), and assign its value to a connection in __construct(), and use this connection link whenever I need it in the class.

    Of course, I'd have to instanciate though, since I'd be using $this->mysql_link.

  • @sachav1993 Actually, this tutorial is a complement of another tutorial I will be releasing soon that has to do with the PDO connection class. I stated this in the tutorial at 0:50. It will be replacing the normal mysql_connect with a PDO class object. This object will check whether a connection has already been established. If so then return the connection reference. If not, then return a new connection.

  • @sachav1993 Also, the mysql connection automatically closes at the end of every script. So, the use of mysql_connect in this static class is no different if you were to use mysql_connect at the top of a script. Hence,there is no extra resources being used. The method used in the tutorial allows programmers to limit the amount of code duplication and increases code re-usability, efficiency, and simplicity.

  • @sachav1993 There is also the problem of having to instantiate an object every time you want to connect to a database. It is conventionally approached that way, but as you start to learn more about OO PHP, you will realize that it is not a flexible approach. Remember, just because it OO PHP doesn't mean that everything has to be an object. There are static, abstract, interfaces, etc that a lot of people do not know how to really use.

  • First I want to say how helpful these tutorials are. It is very difficult to extrapolate information from books on OOP PHP. Hey, is "DataManager::_DBConnect" the same as say "$a = new DataManager(); $a->_DBConnect" or "self::_DBConnect" the same as "$this->DBConnect()"?

  • @budarmstrong Hello, thank you for your question. Unfortunately, you cannot access dbConnect() function through a class object because it is a static function. Static functions are usually known as standalone class functions that do not depend on any class methods or class members. Static function are easier to access because you do not need to create a class object to access them. I hope that answered your question.

Top Comments

  • please - change keyboard!!

    with some softer button pushing.

    absolutely needed!

  • You have officially half opened up another world to me... Can you please keep this goin?

see all

All Comments (30)

Sign In or Sign Up now to post a comment!
  • very nice tutorial and I really hard to find such tutorials like this, I am almost finishing my frist book about php and databases, so after that I am gonna watch all video you are published here, I am sure all of them are powerful like this one, thanks

  • Hi! PHPClass, Thanks for the tutorial. I just have one question for you. I wanted the _DbConnect() to read values passed in from a config.php file. For some reason, I can't get the class to read the values.

    $settings['db_host']; $settings['db_name']; $settings['db_user']; and $settings['db_pass']; I have this at the top of the php script before the class. require_once 'config.php';

    Thanks

    Joe

  • Thx. You should cap this example off by making the table-name and column-names as parameters to the functions -- then people can actually re-use the code w/o modification for *any* db table.

  • Subscribed, -> Nice tut! You're professional php developer. At what university have you been?

  • Will the site be laggy becouse you are connecting to the DB every time you create new function..

  • To find out how many images have you uploaded isn't enough mysql_num_rows()?

    I think it would be more easy than the example that you gave. Nice tutorial though.

  • when do you close the connection to the DB?

Loading...

Alert icon
0 / 00Unsaved Playlist Return to active list
    1. Your queue is empty. Add videos to your queue using this button:
      or sign in to load a different list.
    Loading...Loading...Saving...
    • Clear all videos from this list
    • Learn more