Is it possible to use store_result() and bind_resu

2020-02-01 07:32发布

问题:

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.

回答1:

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!



回答2:

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.



回答3:

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();
}


标签: php pdo mysqli