My question is fairly straightforward: Is it possible to use store_result()
and bind_result()
with PHP PDO?
Here is some example code I came across:
$stmt = $mysqli->prepare("SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1")) {
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
$stmt->bind_result($user_id, $username, $db_password, $salt); // get variables from result.
$stmt->fetch();
I have seen these used in the context of mysqli
, but not with PHP PDO. store_result()
and bind_result()
are referenced in mysqli on www.php.net. I'm curious if they are valid, or if there are comparable methods.
Edit: Obviously there is some translation between the two methods. My assumption is that store_result
and bind_result()
are similar to PDO's fetch()
commands.
Instead of
store_result
andbind_result
; you can use the language constructlist
. An example usage shall be:The
PDO::FETCH_NUM
is used because:It is possible and I have recently used in my project.
The point is, there is absolutely no point in using neither store_result() nor bind_result() with PDO.
Just get your data and use it anywhere you wish. That's the very point of PDO.
Now you have your userdata in
$row
array.Storing resulting row in separate variables is very seldom practice nowadays. But if you prefer such ancient way of dealing with data, you could add
to the above code to get your global variables.
The main problem with mysqli-way is that it is extremely hard to use it with whatever level of abstraction.
In the real life we never call for database in the global scope, but rather in a function like this
and for some reason this simple yet mostly used code become extremely difficult with mysqli! In the real life we don't need separate variables but rather single array to be returned. But with mysqli we need to get these variables first, then put them in array and only then return them. Just crazy!