Skip to main content Skip to course navigation

wpdb class

In normal PHP outside WordPress, you were opening the database connection yourself with mysqli and then running queries from there. In WordPress, that part is already done for you. Once WordPress is loaded, you get access to the global $wpdb object, which is WordPress’s database wrapper.

That means the first mindset shift is this: you are no longer responsible for creating the connection. You are responsible for using the existing WordPress database object correctly.

The second mindset shift is table naming. In your MySQL practice, you were writing table names directly, like practice_users. In WordPress, table names often depend on the site prefix, so instead of hardcoding something like wp_users, you should build it using $wpdb->prefix . 'users'. That way, your code still works if the install uses a custom prefix.

The third mindset shift is query safety. When part of a query is dynamic, like a user ID, you should not concatenate that value directly into the SQL string. Instead, you use $wpdb->prepare(). You write the SQL with placeholders like %d for integers and %s for strings, and then pass the real values separately. So the SQL stays structured, and WordPress safely prepares the final query.

For today, that is enough. The goal is not to run lots of queries. The goal is just to get comfortable with this new shape:

load WordPress -> access $wpdb -> build prefixed table name -> prepare SQL.

Reference: