Lesson 8: Retrieving Auction Records from the Database
Introduction
In the previous lesson, we created our first Auction Manager class and added the create_auction() method. Although the method is capable of inserting auction records into the database, we haven’t used it yet. As a result, our wp_flipnzee_auctions table is still empty.
Before building forms or administration pages, it’s important to learn how to retrieve information from the database.
Professional software is built in layers. First we create the database, then we build classes that communicate with it, and only afterwards do we create user interfaces.
In this lesson, we’ll expand the Auction Manager by adding methods that retrieve auction records from the database.
Learning Objectives
By the end of this lesson, you’ll be able to:
- Retrieve a single auction by its ID.
- Retrieve all auctions.
- Understand WordPress’s
$wpdb->get_row()method. - Understand WordPress’s
$wpdb->get_results()method. - Continue separating database logic from presentation.
Why Read Before Writing?
Most applications spend far more time reading data than writing it.
Think about Flipnzee.
Visitors browse listings.
Buyers search auctions.
Search engines index pages.
Administrators review sales.
All of these activities require reading data from the database.
Learning to retrieve records is therefore just as important as learning to create them.
Step 1 – Open the Auction Manager
Open:
includes/class-auction-manager.php
We’ll keep our existing create_auction() method.
Now we’ll extend the class.
Step 2 – Add a Method to Retrieve One Auction
Add the following method below create_auction().
/**
* Retrieve one auction by ID.
*
* @param int $auction_id Auction ID.
*
* @return object|null
*/
public static function get_auction( $auction_id ) {
global $wpdb;
$table = $wpdb->prefix . 'flipnzee_auctions';
return $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$table} WHERE id = %d",
$auction_id
)
);
}
Understanding get_row()
This method retrieves a single record.
If the auction exists:
Auction #15
it returns one object.
If the auction doesn’t exist:
Auction #999
it returns:
null
Why Use $wpdb->prepare()?
Never insert user input directly into SQL queries.
Instead of writing:
SELECT * FROM table WHERE id = $auction_id
we write:
$wpdb->prepare(...)
WordPress safely prepares the query before sending it to MySQL.
This helps protect your plugin from SQL injection attacks.
Step 3 – Retrieve All Auctions
Now add another method.
/**
* Retrieve all auctions.
*
* @return array
*/
public static function get_all_auctions() {
global $wpdb;
$table = $wpdb->prefix . 'flipnzee_auctions';
return $wpdb->get_results(
"SELECT * FROM {$table} ORDER BY created_at DESC"
);
}
Understanding get_results()
Unlike get_row(),
get_results()
returns multiple rows.
If the database contains:
Auction 1
Auction 2
Auction 3
you receive an array containing three objects.
If there are no auctions yet:
you receive an empty array.
Why Aren’t We Showing Results Yet?
You might wonder:
Why don’t we display these auctions immediately?
Because our plugin still doesn’t have an administration page.
For now, we’re building the backend.
Soon, the administration interface will simply call these methods.
Our Growing Auction Manager
Our class now contains:
create_auction()
get_auction()
get_all_auctions()
Soon we’ll add:
update_auction()
delete_auction()
close_auction()
Notice how everything related to auctions stays inside one class.
Lesson Summary
In this lesson, we expanded the Auction Manager by adding methods to retrieve auction records from the database.
Although our table currently contains zero records, our plugin is now capable of reading both individual auctions and complete auction lists.
This prepares us for building the administration interface in future lessons.
Key Takeaways
- ✓ Use
get_row()when retrieving one record. - ✓ Use
get_results()when retrieving multiple records. - ✓ Always use
$wpdb->prepare()with user input. - ✓ Keep database operations inside dedicated classes.
- ✓ Build the backend before the user interface.
Common Mistakes
- Writing raw SQL with user input.
- Mixing SQL with HTML.
- Forgetting to use
$wpdb->prepare(). - Returning multiple rows when only one is expected.
Git Commands Used
git add .
git commit -m "Lesson 8: Retrieve auction records"
git push
Project Status
✅ Development environment
✅ Plugin skeleton
✅ Plugin installation
✅ Plugin lifecycle
✅ .gitignore
✅ Data architecture
✅ Database table
✅ Auction Manager
✅ Retrieve auction records
⬜ Create admin menu
⬜ Create first auction
⬜ Bid engine
⬜ Escrow workflow
⬜ Website transfer
⬜ Notifications
⬜ Version 1.0
Project Evolution
Throughout this project, we’ve been refining the architecture as new requirements become clearer.
One important decision was separating listing management from auction management. Website information remains the responsibility of Flipnzee Analytics, while Flipnzee Auctions focuses on auction logic and transaction workflows.
This lesson continues that philosophy by keeping all database operations inside the Auction Manager rather than scattering SQL queries throughout the plugin.
As always, the GitHub repository contains the latest implementation, while these lessons document the reasoning behind each architectural decision.
Developer’s Notebook
One of the easiest ways to identify well-structured software is by looking at how it accesses the database. Instead of allowing every part of the application to execute SQL queries, professional developers usually centralize database operations inside dedicated classes. This makes the code easier to test, easier to debug, and much simpler to extend. As Flipnzee Auctions grows to include bidding, escrow tracking, and website transfers, this design will allow us to add new functionality without creating unnecessary complexity.
Looking Ahead
In Lesson 9, we’ll create our first WordPress Administration Menu for Flipnzee Auctions. Instead of interacting with the database directly, administrators will begin managing auctions through the WordPress Dashboard. This marks the transition from backend infrastructure to a real user interface.



