Bird
0
0

Which of the following is the correct way to safely prepare a SQL query using $wpdb?

easy📝 Syntax Q12 of 15
Wordpress - WordPress Query and Database
Which of the following is the correct way to safely prepare a SQL query using $wpdb?
A$wpdb->prepare("SELECT * FROM wp_posts WHERE ID = %d", $id);
B$wpdb->query("SELECT * FROM wp_posts WHERE ID = $id");
C$wpdb->get_results("SELECT * FROM wp_posts WHERE ID = $id");
D$wpdb->execute("SELECT * FROM wp_posts WHERE ID = %d", $id);
Step-by-Step Solution
Solution:
  1. Step 1: Identify the safe query method

    $wpdb->prepare() safely inserts variables into SQL queries using placeholders like %d.
  2. Step 2: Check each option

    $wpdb->prepare("SELECT * FROM wp_posts WHERE ID = %d", $id); uses prepare correctly with %d and variable $id. Others either lack preparation or use wrong methods.
  3. Final Answer:

    $wpdb->prepare("SELECT * FROM wp_posts WHERE ID = %d", $id); -> Option A
  4. Quick Check:

    Safe query uses prepare with placeholders [OK]
Quick Trick: Use prepare with %d, %s for safe queries [OK]
Common Mistakes:
  • Inserting variables directly without prepare
  • Using wrong method names like execute
  • Not using placeholders for variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Wordpress Quizzes