Quick variable and query help

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
stevebluck
Forum Newbie
Posts: 8
Joined: Fri Apr 03, 2009 6:21 am

Quick variable and query help

Post by stevebluck »

Hello,

please take a moment to look at my code

Code: Select all

<?php
 
    if ($location != '')
    { 
        $loc = "AND pm.meta_value = '$location'";
    }
    
    if ($beds != '')
    { 
        $bed = "AND pm.meta_value = '$beds'";
    }
    
    if ($price != '')
    { 
        $pri = "AND pm.meta_value = '$price'";
    }
    
    $query = "SELECT the_posts.*
                     FROM wp_posts the_posts, wp_postmeta pm
                     WHERE the_posts.id = pm.post_id AND
                    
                        the_posts.id IN (
                            SELECT xposts.id
                            FROM wp_posts xposts, wp_term_relationships catspost
                            WHERE catspost.object_id = xposts.id
                            AND catspost.term_taxonomy_id IN ($cat_id)
                    
                     ) AND
                     ((
                      pm.meta_key = 'state_value'
                      I WANT TO PUT THE $LOC VARIABLE HERE
                     ) OR
                     (
                      pm.meta_key = 'price_value'
                      I WANT TO PUT THE $PRI VARIABLE HERE
                     ) OR
                     (
                      pm.meta_key = 'beds_value'
                      I WANT TO PUT THE $BED VARIABLE HERE
                     ))
                     GROUP BY the_posts.id;
                     ";
                            
    $pageposts = $wpdb->get_results($query, OBJECT);
    
    
?>
I simply would like to know how I put these variables inside the SQL, i'm not sure on the syntax. I have tried this:

Code: Select all

$query = "SELECT the_posts.*
                     FROM wp_posts the_posts, wp_postmeta pm
                     WHERE the_posts.id = pm.post_id AND
                    
                        the_posts.id IN (
                            SELECT xposts.id
                            FROM wp_posts xposts, wp_term_relationships catspost
                            WHERE catspost.object_id = xposts.id
                            AND catspost.term_taxonomy_id IN ($cat_id)
                    
                     ) AND
                     ((
                      pm.meta_key = 'state_value'
                      ". $loc ."
                     ) OR
                     (
                      pm.meta_key = 'price_value'
                      ". $pri ."
                     ) OR
                     (
                     pm.meta_key = 'beds_value'
                     ". bed ."
                     ))
                     GROUP BY the_posts.id;
                     ";
But with no luck. Please help!

Many thanks.
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Quick variable and query help

Post by Robert07 »

Have you tried to echo the $query after defining it so you can make sure it is created the way you want it to be? If you echo it, then run that query in phpmyadmin or the mysql command line, what is the result?
Post Reply