Reading from a Database

Now that you can add data to your database, you need to find a way to fetch it. You can extract an individual element from the database with the dba_fetch() function. dba_fetch() requires the name of the element you want to access and a valid DBA resource, and it returns the value you are accessing as a string. So, to access the price of the "Tricorder" item, you would use the following code:

$price = dba_fetch( "Tricorder", $dbh );

If the "Tricorder" element does not exist in the database, dba_fetch() returns false.

You won’t always know the names of all the keys in the database, however. What would you do if you needed to output every product and price to the browser without hard-coding the product names into your ...

Get Sams Teach Yourself PHP in 24 Hours, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.