6

So I have been developing plugins that create interact and delete to a database by using the get_option functions.

I have now seen some tutorials showing how to use the global $wpdb way of getting values from the database.

What is the difference between the two, and is one better than the other?

5 Answers 5

4

For storing plugin options or light weight data related to posts, get_option(), get_post_meta(), and their related functions are ideal. For relational database activity, $wpdb is best choice. Here's why:

$wpdb is a class based on the ezSQL PHP class for interacting with the database. Some features include:

1) provides SQL injection protection using the $wpdb->prepare(), $wpdb->insert(), and $wpdb->update() methods. get_option() is a helper function that allows you to do a Key => Value pair.

2) $wpdb is easy to use. It can return record sets in various forms: $wpdb->get_results($sql, ARRAY_A) an Array or Associative Arrays containing the returned rows with the column names being the keys. $wpdb->get_results($sql) would return an array of object with the column name as properties of the object. $wpdb->get_var($sql) would return a scalar result (the first column of the first row of the data set from the query). $wpdb->get_row($sql) would return a single row as an object.

3) $wpdb allows you to interact with any table in the database, even performing free form queries using $wpdb->query($sql)

4) WordPress will likely insure that your interactions with $wpdb will not need to change if they add support for databases other than MySQL. The original ezSQL class was intended to provide some cross database support.

So, if you're needing to deal with data in a relational way, $wpdb is really an excellent choice for WordPress.

get_option() and get_post_meta() provide an easy way of dealing with small amounts of data, either relating to a specific post in the case of get_post_meta() or as a Key => Value pair with get_option().

One of the nice things about these is that you can save a serialized array or object and get that data back out as either an array or object. This gives you a very easy way to deal with fields of data as if you had a database table. However, this doesn't work well if you need to relate data between tables or do summing, counting, or other database calculations on the serialized data. I those cases, a fully fledged table and $wpdb would better serve.

3

Using WordPress helper functions (not limited to get_option()) will ensure your plug in to be forward compaitable when newer version of WordPress made changes that may potentially effect your code.

You are recommanded to understand and use their helpers before considering coding your own.

1
  • Yeah like its advised to use get_option() in loops, so that instead of one call to the db you get like 20 calls.
    – Tosh
    Dec 4, 2012 at 20:46
0

The global $wpdb variable is more powerful than the get_option() function as it allows you to manipulate and retrieve data from any table in wordpress, including all plugin tables. This gives you more power than simply gaining access to the options table.

You should use get_option() when you are specifically retrieving an option from the options table, used for plugin options. Other variations of this function are add_option, update_option, and delete_option. All of the related functions should be used specifically for plugin options.

0

I've used both before. If you can use the helper functions, you should do so. If you need to do something very custom you can use the global. I only use the global if I just must write my own query

1
  • I wrote a plugin back in 2.5 using global $wpdb, site owner decide to upgrade to 2.6 without my concern and whole thing turn chaotic as revisions were introduced and copies of pages/posts gone wild. There are many more minor examples that throw me away from $wpdb
    – Trav L
    Jan 6, 2010 at 23:46
0

I use both in my development.

  • get_option() is far easier for static or single info bites
  • $wpdb is better for storing multi-dimensional information

The primary difference is that for grab-and-go information (like license keys, expiration dates, and static info) get_option() is really handy. It's trim, fast, and really easy to use.

But, I develop plugins that manage user data and form submissions, and in cases like this get_option() doesn't offer the versatility I need without writing a lot of compound arrays or trying to track multiple options. For multi-dimensional information, or for cross-referencing related data entries for a plugin, $wpdb is much better - you can structure your own tables and sort/organize how you like.

:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.