Diving into PHP: Day 16

Continuing on with our OOP review, we’ll begin to build a real-world database abstraction class. We’ll review the __construct magic method, as well as the mysqli object.

Day 16: Database Abstraction Class

Download the Source Code


  • Skofus says:

    i Agree with bob, whats happening? Anyway, i don’t know if this is useful for someone but i use it alot. Works great and save me alot of time.

  • Skofus says:

    lol, forget about my last “Anyway, i don’t know if this is useful for someone but i use it alot. Works great and save me alot of time.”

  • Meshach says:

    We’re waiting Jeffrey/Drew. ;)

  • Mike says:

    is there ever going to be another one of these? or did this series die (again)?

  • Kevin says:


    Using your technique, you’re only echoing one column. but what if I have more than one column to output? Like title, contents etc??

    Can someone help me with this?

  • Meshach says:

    Jeffrey… Is this the end of the series?

    What’s going on? Please update us on this…

  • Meshach says:

    I emailed Jeffrey about this and never got a response… I guess this series is dead.

    It’s a shame.

  • Mike says:

    Yes, it is very unfortunate, when this originally started and died I was disappointed and when I saw that Jeffrey was starting it over again I had high hopes but once again it has faded away……hopefully someone can pick up this series and keep it going.

  • jason says:

    I noticed in one of your other screen cast that you mentioned that using a msql_connect is not the correct way to do it. What are the pros and cons?

  • Sean says:


    I have the same problem! I want to get the information from different field in the table, not just one.

    Was hoping some new videos would help out but seem like none are coming :(

    Was good while it lasted!!

  • leo rapirap says:

    Hi Jeff, is this the end of the screencast?

    I’m hoping would last til Day 20.
    I just love this series and kept on waiting for the next part. :(

    You must have been very busy doing other screencast,
    love CodeIgniter from Scratch series!

    More power to you Jeff!

  • Luis says:

    Hi Jeff! Before all thanks for this great tutorials.

    I have a problem..

    With this example we can extract a specific column from the database and show it but.. what about two different columns..

    I used to do a mysql_fetch_object() and request the values as $row->value1

    But what about: ‘SELECT col1, col2 FROM mytable’

    We are returning a fixed column in this class…. $row->text(i think)

    but I want to return $row->whatever as usual…

    Thank you!

  • Luis says:

    Ouch! I’ve just solved doing this:

    if(($result = $this->mysql->query($sql)) != NULL) {
    $row = $result->fetch_object();
    return $row;
    }#end if result

    The bad thing is that I have to perform the while from the outside.

    But… still working on it! :P

    Thank you anyway.

  • James says:

    @Kevin and Sean

    I’m brand new to coding myself and ran into this same problem. I solved it by returning a multi-dimensional array. While I’m not sure if it’s the best way to do it, it certainly worked for what I wanted to do and was pretty simple. For example, using the same structure from this tutorial:

    function query($sql) {
    if(($result = $this->mysql->query($sql)) != NULL) {
    // echo $result->num_rows;
    while($row = $result->fetch_object()) {
    $x[] = $row->text;
    return $x;
    else {
    echo $this->mysql->error;


    … say you have 5 fields in each row and wanted to return all of those values. Just set the array $x[] equal to an array of your desired fields.

    It would look like the following:

    x[] = array ($row->field1, $row->field2, $row->field3, $row->field4, row->field5);

    (now, like I said, I’m brand new at this, so you may need single quotes around each of those entries, I don’t quite remember what the rule is with variables etc.) that will return an array for each row in your database which itself will be populated with an array of each of the fields you’ve specified.

    To access individual pieces of data within the multi-dimensional array, just use the standard selection notation – $variable[0][1], etc.

    Hope this helps solve your problem, or at least puts you on the right track!



  • DJ says:

    I use php here and there and usually I use require_once for db files. Is it better to use the require and the destruct method for class structure instead of the require_once to cancel the dbl connection?

  • Zac says:

    Im obviously not smart enough for this. its easy to do this proceduraly.

    $r = @mysqli_query ($dbc, $q);
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    echo $row['name'];
    echo $row['fieldTwo'];

    that makes sense. but running the while in a function then returning it….how do you go and get the results? i can see how it works if you need the data for one single column then running a foreach to echo the results. but what if you need a bunch of columns? am i just missing something or am i dumb?

    when i do it proceduraly once the while loop starts i can put in all the html i want and it’ll just run perfectly looping through all the returned rows. but this….i think im just dumb.

  • Carlos says:

    Hey I really enjoyed this tutorial, but when I uploaded the files it gave me an error.

    Fatal error: Class ‘mysqli’ not found in line 8

    I have no idea what this means.

    Thanks in advance!

  • Daoro says:

    This serie was very well done and instructive, thank you !
    I hope that you’ll be producing some more :)