Prepared Statements - Returning recordset

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
jess.albert
Forum Newbie
Posts: 1
Joined: Thu Apr 09, 2009 11:19 am

Prepared Statements - Returning recordset

Post by jess.albert »

So I'm trying to get into using prepared statements via the mysqli interface, since by all accounts that is the best practice for doing queries now. I'm used to doing this the old fashioned way, and I have a function that simply executes a query and then returns the recordset. I'd like to see how to do this with a prepared statement but I can't figure out how to get it to return the correct thing...

Here's the original function:

Code: Select all

 
function getPerson($ageMin, $ageMax)
{   
    $sql = sprintf("SELECT uid AS data, " .
        "first_name AS label " . 
        "FROM person WHERE age >= %d AND age <= %d",
        $ageMin, $ageMax);
            
    $query = mysql_query($sql);
    return $query;
}
 
Basically, here's the prepared statement version that I've worked up and I'm just trying to figure out what exactly the equivalent of return $query; would be:

Code: Select all

 
function getPerson($ageMin, $ageMax)
{   
    global $mysqli;     
    $statement = $mysqli->prepare("SELECT uid AS data, " .
        "first_name AS label " . 
        "FROM person WHERE age >= ? AND age <= ?");
 
    $statement->bind_param("ii", $ageMin, $ageMax);
    $statement->execute();
    //return what?
}
 
Thanks for any help! I appreciate it!
-Jesse
nyoka
Forum Commoner
Posts: 45
Joined: Thu Apr 09, 2009 12:53 pm

Re: Prepared Statements - Returning recordset

Post by nyoka »

Checkout the following functions:

Code: Select all

 
$statement->bind_result();
$statement->fetch();
$statement->close();
 
Post Reply