I switched from mysqli to PDO and I can’t get Row

Asked

Viewed 98 times

2

In my mysqli it was easy

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

How to make it work on PDO ?

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
  • 1

    I think you’re confused buddy :)

  • You overbought $Row and lost the information you were interested in

  • I’ve already removed this variable

  • As you expect only one row of database tries to overwrite fetchAll by just fetch.

  • exactly what @rray replied

  • I’ll try this tomorrow

  • Note: For those who were surprised by my first comment, it actually refers to the first version of the question http://answall.com/revisions/58469/1 :) Good evening

Show 2 more comments

1 answer

5


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

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

In that case to return only one record use fetch() or use your array’s Intel Zero.

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
  • thanks for the good answer, I also changed my sql connection that with it was not working :s / $stmt = $Pdo->prepare("SELECT Name, password FROM login WHERE Email=:email AND password=:password"); for SELECT *

Browser other questions tagged

You are not signed in. Login or sign up in order to post.