I'm converting some old work to mysqli, and having a bit of trouble with Prepared Statements.

In the stripped down code sample below, I'm able to confirm that the code below pulls the records; line 8 echoes the string as expected and if I replace the id parameter with something like lastname = smith to pull multiple records I get the correct number of lines echoed. This suggests that my query and prepared statement are working properly.

Problem is, I don't know how to access the data. Stupid, I know. I did this sort of thing all the time in the old mysql extension and something like $row['lastname'] gave it up, but in mysqli I haven't a clue.

Can someone help me out here?

if ($stmt = $db -> prepare("SELECT * FROM members WHERE id = ?")) {
    // bind parameters, execute the statement, ...
    $stmt -> bind_param("s", $id);
    $stmt -> execute();
    $stmt -> bind_result($result);
    // fetch the value
    while($stmt -> fetch()){
        echo "...<br />";
    }
    // close statement 
    $stmt -> close();
}

I'm not sure but I think this is what you are looking for: fetch_array().
Use something like this:

$row = $stmt->fetch_array(MYSQLI_ASSOC);
echo $row['lastname'];

Click the link for explanation. Hope that helps.

Thanks, v1shwa, but I can't seem to see how I can fit that into the code above to produce results. ;{

Member Avatar for diafol

It's all there in the manual as linked above ^^.

You have a choice:

fetch_array()
fetch_row()
fetch_all()

as well as a few others. BUT some of these functions are only available with mysqlnd (native driver). The rationale behind hosts not supporting this is beyond me, but I wasted hours and hours trying to get fetch_all() to work on my production site. It was only later I discovered that mysqlnd was not supported.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.