0
0
Wordpressframework~5 mins

Direct database queries (wpdb) in Wordpress - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is wpdb in WordPress?
<p><code>wpdb</code> is a WordPress class that helps you safely run database queries. It connects to the WordPress database and lets you get, insert, update, or delete data.</p>
Click to reveal answer
beginner
How do you safely prepare a SQL query using wpdb?

You use the $wpdb->prepare() method. It replaces placeholders with safe values to avoid SQL injection.

Example: $wpdb->prepare('SELECT * FROM wp_posts WHERE ID = %d', $post_id);

Click to reveal answer
beginner
Which wpdb method would you use to get a single row from the database?

You use $wpdb->get_row(). It returns one row as an object, array, or associative array.

Click to reveal answer
intermediate
What is the difference between get_results() and get_var() in wpdb?

get_results() returns multiple rows as an array of objects.

get_var() returns a single value from the database, like one cell.

Click to reveal answer
beginner
Why should you avoid directly inserting variables into SQL queries without prepare()?

Directly inserting variables can cause SQL injection, a security risk where attackers can run harmful commands. prepare() safely escapes variables to prevent this.

Click to reveal answer
Which wpdb method is used to safely insert data into the database?
A<code>query()</code>
B<code>insert()</code>
C<code>prepare()</code>
D<code>get_row()</code>
What placeholder should you use in prepare() for a string value?
A%s
B%d
C%f
D%a
Which wpdb method returns a single value from the database?
A<code>get_results()</code>
B<code>get_row()</code>
C<code>insert()</code>
D<code>get_var()</code>
What does $wpdb->query() return after running an UPDATE statement?
ASingle row object
BArray of results
CNumber of rows affected
DBoolean true or false
Why is it important to use wpdb->prepare() before running a query?
ATo prevent SQL injection
BTo format the output
CTo speed up the query
DTo connect to the database
Explain how to safely run a SELECT query using wpdb and retrieve multiple rows.
Think about how to avoid SQL injection and get many rows.
You got /3 concepts.
    Describe the difference between get_row(), get_results(), and get_var() in wpdb.
    Focus on how many rows or values each method returns.
    You got /3 concepts.