Fetch Objects from the Database

Video loading...

Join Drupalize.Me to watch this video

Join today and gain instant access to our entire video library.

Log in Sign up
  • 0:04
    Getting our ship objects is easy!
  • 0:08
    Create a ShipLoader, and call getShips on it.
  • 0:11
    We don't care how ShipLoader is getting these,
  • 0:14
    that's its problem.
  • 0:16
    Hardcoding is so 1990!
  • 0:19
    Let's load objects from the database.
  • 0:22
    We need to get these ships to their battle stations!
  • 0:25
    At the root of your project, open up the resources directory.
  • 0:29
    Copy init_db.php out of there and into the root of your project,
  • 0:35
    and open it up.
Loading ...

Fetch Objects from the Database


So far we've been hard-coding our ships in the ShipLoader class. In this tutorial we'll take things up a notch by fetching that information from a database. First we're going to create a new database to hold our data, using PDO. (If you are not familiar with working with databases in PHP, you can check out the PHP for Beginners Part 3 series, which covers database fun.) Once we have the database created and filled with some ship data, we'll learn how to fetch that data back out again as an object our app can work with.

Log in or sign up to download companion files.

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.