WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to a mysql query that matches a page title. Not sure of a better way to accomplish this task, but I am stuck.

This is my basic test code.

$page_title = wp_title('');
echo $page_title;
$test = $wpdb->get_results("SELECT * FROM $wpdb->park_data WHERE park_name = '"$page_title"' ") or die(mysql_error());  
$row = mysql_fetch_array($test);
echo $row['park_name'];

I keep getting a error on the query. If I just echo wp_title('');, no problem, prints.

Can anyone see what I am doing wrong?

share|improve this question
up vote 1 down vote accepted

I think if you add a couple of periods before and after the php variable in your query, remove the use of mysql_fetch_array, and use get_row(), it should work better. Here's how I would get a park from your custom table:

$page_title = wp_title('');
$park = $wpdb->get_row("SELECT * FROM $wpdb->park_data WHERE park_name='".$page_title."'");  
   print_r($park); // because other than it being an object, I'm not exactly sure what's returned.

Also, keep in mind that the mysql query assumes the wp_title() returns the page title formatted exactly the same as the park_name stored in the DB. You may need to format wp_title() to get it to match up with your table data.

It looks like you just want one park returned so I used get_row() instead: http://codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Row

share|improve this answer
Thank you sir! You saved me a lot of effort... :) – otinanai Feb 28 '15 at 18:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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