Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

<?php
$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
add comment

1 Answer

up vote 0 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:

<?php
$page_title = wp_title('');
$park = $wpdb->get_row("SELECT * FROM $wpdb->park_data WHERE park_name='".$page_title."'");  
if($park)
{
   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
add comment

Your Answer

 
discard

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.