mysql query from wordpress page using custom table

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.

Read More
<?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?

Related posts

Leave a Reply

1 comment

  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:

    <?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