0

I am actually using this to store and know some fields of my database:

$conn_2 = dbConnect();
$stmt2 = $conn_2->prepare("SELECT firstname, lastname, type FROM BrokerMaster.users WHERE email = ?");
$stmt2->bind_param("s", $email);
$stmt2->execute();
$stmt2->bind_result($firstname, $lastname, $type);
while ($stmt2->fetch()) {
       printf("%s %s %s\n", $firstname, $lastname, $type);
}

But I would like to do something like:

$stmt2 = $conn_2->prepare("SELECT * FROM BrokerMaster.users WHERE email = ?");
$stmt2->bind_param("s", $email);
$stmt2->execute();
$stmt2->bind_result($result); //??
while ($row = $stmt2->fetch()) {
       $firstname = $row["firstname"]
}

I couldn't find a way to do it object oriented. The problem I found is that the $result is not a mysqli_result class (if I am not wrong) and unlike query() the execute() and bind_results() don't create it. (I also couldn't manage to use this answer

What are my mistakes (or misunderstandings)? How can I do it?

Community
  • 1
  • 1
llrs
  • 3,308
  • 35
  • 68

1 Answers1

0

Sure you can, but you would use fetch_assoc() function.

So you use fetch_assoc() on the mysqli_result. Doing it in a while loop, will continue cycling until there are available rows.

while ($row = $result->fetch_assoc()) {
    //Use $row["column_field"];
}

Edit It happens that we can't get result object straight from a prepared statement.

$stmt2->store_result(); 
$result = $stmt2->get_result();

Now you should be able to use fetch_assoc() over your mysqli_result

fillobotto
  • 3,698
  • 5
  • 34
  • 58
  • It doesn't solve the issue, I don't use `$mysqli->query` to avoid MySQL injection, so I can't obtain a $result with `$mysqli->query($query)`. and binding it doesn't return a mysqli_result object – llrs Feb 02 '16 at 11:23
  • @Llopis I have updated the answer. These are the only information I could find and I can't test it right now – fillobotto Feb 02 '16 at 13:00
  • 1
    `get_result` return a [resultset](http://php.net/manual/en/mysqli-stmt.get-result.php) which I don't know if it is a mysqli_result, as far as I tested it isn't. Thanks for your effort. – llrs Feb 02 '16 at 13:04