Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use store_result() and bind_result() with PHP PDO?

Tags:

php

pdo

mysqli

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.

like image 371
Mlagma Avatar asked Jun 04 '13 01:06

Mlagma


3 Answers

Instead of store_result and bind_result; you can use the language construct list. An example usage shall be:

$stmt = $pdo->prepare("SELECT id, username, password, salt 
    FROM members 
    WHERE email = ? 
    LIMIT 1")
$stmt->bindValue( 1, $email, PDO::PARAM_STR);
$stmt->execute();
list($user_id, $username, $db_password, $salt) = $stmt->fetch( PDO::FETCH_NUM );

The PDO::FETCH_NUM is used because:

list() only works on numerical arrays and assumes the numerical indices start at 0.

like image 145
hjpotter92 Avatar answered Nov 12 '22 05:11

hjpotter92


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.

$sql  = "SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($email));
$row  = $stmt->fetch();

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

extract($row);

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

function getUserData() {
    // getting data
    return $array;
}

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!

like image 30
Your Common Sense Avatar answered Nov 12 '22 05:11

Your Common Sense


It is possible and I have recently used in my project.

$prep_stmt = "SELECT id FROM member WHERE username = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);

if ($stmt) 
{
    $stmt->bindparam(1, $username);
    $stmt->execute();
    $result=$stmt->fetchALL(PDO::FETCH_OBJ);
}
if ($stmt->rowCount() == 1) 
{
    echo"user exist";
    $stmt->close();
}
like image 28
x00 Avatar answered Nov 12 '22 07:11

x00