Alternative to mysql_real_escape_string

When working with database in WordPress you should never use the low lever mysql_* or mysqli_* functions.

Always use $wpdb methods, in your case you should use prepare():

$query = $wpdb->prepare(
  "SELECT post_title from $wpdb->posts
  WHERE post_title LIKE %s",
  "%" . $myTitle . "%"
);

Moreover, once you are getting a single column, you have easier life using get_col instead of get_results:

$myposttitle = get_col( $query );

Leave a Comment