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?

1 Answer 1


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


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.