I changed mysqli to PDO and I can not get row

2

In my mysqli it was easy

$_SESSION['userphoto'] = $row['userphoto'];

How to make PDO work?

ConnectionLogin.php

session_start();

$stmt->bindParam(':email', $_POST['email']);
$stmt->bindParam(':senha', md5($_POST['senha']));
$stmt->execute();
$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

if($row > 0){
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['userphoto'] = $row['userphoto'];

    //$_SESSION funcionava certo no MYSQLI
    
asked by anonymous 15.04.2015 / 01:22

1 answer

5

fetchAll () returns an array with all the records in the query, it is in this format: / p>

array(0 => array('email' => 'email1', 'senha' => 'senha1'),
      1 => array('email' => 'email2', 'senha' => 'senha2));

In this case, to return only one record use fetch () or use the zero index of your array .

Option 1 - Recommended

$row = $stmt->fetch(PDO::FETCH_ASSOC);

if(count($row) > 0){
   $_SESSION['email'] = $_POST['email'];
   $_SESSION['userphoto'] = $row['userphoto'];

Option 2

$row = $stmt->fetchAll(PDO::FETCH_ASSOC);

if(count($row) > 0){
    $_SESSION['email'] = $_POST['email'];
    $_SESSION['userphoto'] = $row[0]['userphoto']; //usando indice zero
    
15.04.2015 / 03:27