Alert icon
We're changing our privacy policy. This stuff matters.  Learn more  Dismiss

PHP Database Object with INI Parsing

Loading...

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

Uploaded by on May 14, 2010

IMPORTANT: In the example I forgot to autoload() or include the objects in index.php :-) So do that if you do type this out!

This is a quick tutorial on writing a Reusable Database Object, which extends a Database Properties Class, Which reads an INI file. In the real world you'd probably want to .htaccess protect the directory your INI files are listed. Also, this isn't a robust nor impressive Class, it's to give you a starting block to create your own DB object.

You don't need to have a separate DatabaseProperties Object, but I think it's a good idea because your actual Database Class may eventually handle different databases or something, who knows really, I suppose it's a preference.

Thanks to my friend David M. who's taught me a lot, even though he would critique this entire video to the death and tell me to redo it a much better way after lecturing me for a few hours LOL, then I'd have to learn something entirely new - As long as he doesn't see this video there won't be any problems, HAHA.

NOTE: I had to edit a few parts to get it under 10 minutes so if it skips a little no worries :P

  • likes, 0 dislikes

Link to this comment:

Share to:

Uploader Comments (JREAMdesign)

  • I'm using Eclipse and my file structure looks like: (root being htdocs)

    root/projectfolder/index.php

    root/projectfolder/includes/al­lclasses.php

    root/projectfolder/adminpage.

    My question: can i use include $_SERVER[DOCUMENT_ROOT]. 'file path to classes folder'; to set up the link then just create my objects as usual? I'm new to OOD. thx

  • @wrdeboise You should be able to do that just fine! I cant remember exactly what i said in this tutorial, but if it was about "autoloading", then at some point you will wanna autoload, saves time :)

  • I have a question, in index.php wouldn't I have to include DatabaseProperties.php and Database.php. Why not include DatabaseProperties.php inside of Database.php. Otherwise after you have all these classes that you have to include on all these different files/pages. Am I right or am I wrong? I think I'm just missing something. Anyway thanks for the video you should keep making them.

  • @XtronGraphics Heya, I mentioned in the comments I forgot to autoload or include if you saw that :P -- Well the reason I personally don't think you should do the include through the class php file is because at some point you are going to want to autoload() all your classes, and those can only be class files. A class should be standalone and not interfere with the flow of loading. When you separate components it makes your application really easy to work with later on and if you re'use it :)

  • your videos are awesome. It would be cool to see how you would approach creating a website witth OOP. I have been trying to figure out the best way, but always resort to procedural programming. I like OOP better though

  • @divac7777 Hiya, I'm not sure what kind of website you want to make, because for a regular website I would just use a pre-built CMS such as WordPress, or FrogCMS. It sounds like you want more of an "Application Website". Procedural programming isn't bad, but it's hard to explain everything in 10 minutes or a short comment, and I don't know everything, haha. Well I hope some videos will be able to help I do them once in a while!

see all

All Comments (10)

Sign In or Sign Up now to post a comment!
  • @JREAMdesign Never mind. For everyone following along, make sure you surround the values in the ini file with double quotes. I was pulling my hair out because I was using single ( ' ') quotes.

  • @JREAMdesign Ok, I fixed the problem with the extends problem -- it needed an 'include_once DatabaseProperties in my Database class --, but now it won't connect to my database unless I put all the values directly in mysqli_connect method. Basically it's not getting the required info from DatabaseProperties class which is parsing the .ini file. My code looks exactly like users so I don't know what gives. Any help would be greatly appreciated.

  • I stripped down my code to just a basic class with a _construct then echo 'hello'; and while eclipse is showing these pieces are put together properly, when I create an index with include 'Database.php' then invoke the constructor $db = new Database(); and navigate to that directory with the index and classes in my browser the screen is blank. No error no nothing. What gives? Is there a set up problem or something?

  • Fatal error: Class 'DatabaseProperties' not found in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\vi­deo_dating\includes\Database.p­hp on line 3

    I put all the files in one directory and called it 'includes' I created an index and tried declaring my new Database object. Line 3 happens to be the include statement: include 'Database.php';

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