Page 1 of 1

Prepared Statements - Returning recordset

Posted: Thu Apr 09, 2009 11:28 am
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

Re: Prepared Statements - Returning recordset

Posted: Fri Apr 10, 2009 6:19 pm
by nyoka
Checkout the following functions:

Code: Select all

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