Page 1 of 1

Trying to write a PHP Function to get MySQL Data

Posted: Tue Mar 09, 2010 8:29 pm
by ChrisF79
Greetings:

I'm trying to write a function to do my SQL query for me so I can call it without cluttering up my code. The problem is, it simply isn't working. Here's the code that I have:

Code: Select all

 
function property_query($mls) {
    $query = "SELECT
            l.mls,
            l.list_price,
            l.listing_status,
            l.street_number,
            l.street_name,
            l.street_type,
            l.street_direction,
            l.unit_number,
            l.city,
            l.shortzip,
            l.subdivision_id,
            l.furnished,
            l.listing_long_description,
            l.elevator,
            l.year_built,
            l.beds_id,
            l.unit_floor,
            l.total_floors,
            l.full_baths,
            l.half_baths,
            l.sqft_living,
            l.sqft_total,
            l.garage_spaces,
            l.carport_spaces,
            l.private_pool,
            l.private_spa,
            l.acres,
            l.waterfront_description,
            l.boat_access,
            l.view,
            l.construction,
            l.roof,
            l.flooring,
            l.cooling,
            l.heating,
            l.irrigation,
            l.pets,
            l.hoa_fee,
            l.master_hoa_fee,
            l.condo_fee,
            l.broker_code,
            l.office_name,
            l.foreclosed,
            l.shortsale,
            l.broker_reciprocity,
            l.lat,
            l.lng,
            l.num_images,
            
            sub.id,
            sub.subdivision,
            sub.development,
            
            beds.beds_id,
            beds.beds_desc,
            
            status.listing_status_id,
            status.listing_status_desc
            
        FROM listings AS l,
            subdivisions AS sub,
            bedrooms AS beds,
            listing_status AS status
            
        WHERE l.mls = '".$mls."'
        AND l.subdivision_id = sub.id
        AND l.beds_id = beds.beds_id
        AND l.listing_status = status.listing_status_id
        ";
    $result = mysql_query($query,$dbh);
         return $result;
}
 
Once I run this query, I try to do a while ($data = mysql_fetch_array($result)) { ... do stuff here ... } but the do stuff here part doesn't work!

The only thing I can think of is that it isn't finding the $dbh variable. That's included in a different file. Do I need it?

Any help from the experts would be greatly appreciated! Thank you!

Re: Trying to write a PHP Function to get MySQL Data

Posted: Tue Mar 09, 2010 8:35 pm
by w1n78
ya it looks that way. try to include

global $dbh;

inside the function

Re: Trying to write a PHP Function to get MySQL Data

Posted: Wed Mar 10, 2010 5:55 pm
by ChrisF79
I tried adding the global $dbh and I even put it all in the same file to make sure there wasn't any error with an include. It still doesn't work though. Here's the code I have now if anybody can help. I'm just getting a blank page when I run it.

Code: Select all

function property_query($mls) {
    global $dbh;
    $query = "SELECT
            l.mls,
            l.list_price,
            l.listing_status,
            l.street_number,
            l.street_name,
            l.street_type,
            l.street_direction,
            l.unit_number,
            l.city,
            l.shortzip,
            l.subdivision_id,
            l.furnished,
            l.listing_long_description,
            l.elevator,
            l.year_built,
            l.beds_id,
            l.unit_floor,
            l.total_floors,
            l.full_baths,
            l.half_baths,
            l.sqft_living,
            l.sqft_total,
            l.garage_spaces,
            l.carport_spaces,
            l.private_pool,
            l.private_spa,
            l.acres,
            l.waterfront_description,
            l.boat_access,
            l.view,
            l.construction,
            l.roof,
            l.flooring,
            l.cooling,
            l.heating,
            l.irrigation,
            l.pets,
            l.hoa_fee,
            l.master_hoa_fee,
            l.condo_fee,
            l.broker_code,
            l.office_name,
            l.foreclosed,
            l.shortsale,
            l.broker_reciprocity,
            l.lat,
            l.lng,
            l.num_images,
            
            sub.id,
            sub.subdivision,
            sub.development,
            
            beds.beds_id,
            beds.beds_desc,
            
            status.listing_status_id,
            status.listing_status_desc
            
        FROM listings AS l,
            subdivisions AS sub,
            bedrooms AS beds,
            listing_status AS status
            
        WHERE l.mls = '".$mls."'
        AND l.subdivision_id = sub.id
        AND l.beds_id = beds.beds_id
        AND l.listing_status = status.listing_status_id
        ";
    
    return $query;
}
    
 
    property_query($mls);
    echo $query;
 

Re: Trying to write a PHP Function to get MySQL Data

Posted: Thu Mar 11, 2010 12:09 am
by realnsleo
hi! i dont see any query operation in your code. should it be something like:

Code: Select all

function property_query($mls) {
     global $dbh;
     $query = "blah blah blah ...";
     
     return mysql_query($query, $dbh); // for mysql or $dbh->query($query) for mysqli
 }