Trying to write a PHP Function to get MySQL Data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChrisF79
Forum Commoner
Posts: 26
Joined: Tue Apr 01, 2008 8:26 pm

Trying to write a PHP Function to get MySQL Data

Post 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!
w1n78
Forum Newbie
Posts: 12
Joined: Mon Mar 08, 2010 10:55 pm

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

Post by w1n78 »

ya it looks that way. try to include

global $dbh;

inside the function
ChrisF79
Forum Commoner
Posts: 26
Joined: Tue Apr 01, 2008 8:26 pm

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

Post 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;
 
realnsleo
Forum Newbie
Posts: 15
Joined: Sat May 16, 2009 12:08 pm

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

Post 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
 }
Post Reply