0
0
Wordpressframework~10 mins

Direct database queries (wpdb) in Wordpress - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to get all rows from a custom table using $wpdb.

Wordpress
$results = $wpdb->[1]("SELECT * FROM wp_custom_table");
Drag options to blanks, or click blank then click option'
Aupdate
Bquery
Cinsert
Dget_results
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'query' instead of 'get_results' returns number of rows, not data.
Using 'insert' or 'update' methods for SELECT queries.
2fill in blank
medium

Complete the code to safely prepare a query with a variable using $wpdb->prepare.

Wordpress
$sql = $wpdb->prepare("SELECT * FROM wp_users WHERE ID = [1]", $user_id);
Drag options to blanks, or click blank then click option'
A%d
B%s
C%f
D%u
Attempts:
3 left
💡 Hint
Common Mistakes
Using %s for numbers can cause SQL errors or injection risks.
Using %f for integers is incorrect.
3fill in blank
hard

Fix the error in the code to insert a new row safely using $wpdb.

Wordpress
$wpdb->[1]('wp_custom_table', array('name' => $name, 'age' => $age), array([2], [3]));
Drag options to blanks, or click blank then click option'
Ainsert
Bupdate
C%s
D%d
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' instead of 'insert' for adding new rows.
Wrong format placeholders cause data type errors.
4fill in blank
hard

Fill both blanks to update a user's email safely using $wpdb.

Wordpress
$wpdb->[1]('wp_users', array('user_email' => $email), array('ID' => $user_id), array([2]), array([3]));
Drag options to blanks, or click blank then click option'
Aupdate
Binsert
C%s
D%d
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'insert' instead of 'update' to modify data.
Mixing up the order of format placeholders.
5fill in blank
hard

Fill all three blanks to delete a user by ID safely using $wpdb.

Wordpress
$wpdb->[1]('wp_users', array('ID' => [2]), array([3]));
Drag options to blanks, or click blank then click option'
Adelete
B$user_id
C%d
Dupdate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'update' instead of 'delete' to remove data.
Not using the variable for the ID in the where clause.