Mohammed Imran
Mohammed Imran

Reputation: 21

PHP PDO fetch Object convering to string

try {
    $dbh = new PDO($dsn, $user, $pass);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}
// For brevity, code to establish a database connection has been left out

$sth = $dbh->prepare('
  SELECT
    Password
  FROM Customer_login
  WHERE
    Email = :username
  LIMIT 1
  ');

$sth->bindParam(':username', $username);

$sth->execute();

$user = $sth->fetch(PDO::FETCH_OBJ);
printf($user);
// Hashing the password with its hash as the salt returns the same hash
if ( hash_equals($user->Password, crypt($password, $user->Password)) ) {
  // Ok!
printf("login success for customer");
echo 'Login Accepted';
}
else
{
printf("customer login fail");
}
?>

When I run this code the error says Object of class stdClass could not be converted to string

I'm actually trying fetch hashed password from table using email ID and trying compare the credentials given by the yes.

Also can anyone explain what is best way to implement IOS applications when we store data in the external server.

Upvotes: 1

Views: 102

Answers (1)

Clay
Clay

Reputation: 4760

Remove the line with printf. The value of $user is an object yet the first parameter of printf excepts a string.

printf: int printf ( string $format [, mixed $args [, mixed $... ]] ) Read more here

As for your second question, that is a fairly broad question and you might want to research that a lot more to see what your options are and evaluate what will work the best for you. You might like Parse.com but you might also want to setup your own server, too many choices.

Upvotes: 1

Related Questions