Cannot use object of type PDOStatement as array in

6208 views php
0

I have this Error: Fatal error: Uncaught Error: Cannot use object of type PDOStatement as array in C:\xampp\htdocs\seth\Plugins\login\php\completedetails.php:28 Stack trace: #0 {main} thrown in C:\xampp\htdocs\seth\Plugins\login\php\completedetails.php on line 28

it some days ago was functiones but now it pass, ¿what I should do?, Im leaving the code down

session_start();

$_SESSION['id'];
$id = $_SESSION['id'];
$name = $_POST['name'];
$password = $_POST['password'];
$password = password_hash($password, PASSWORD_DEFAULT); 
$team = $_POST['team'];


    if($name == "" && $password == "" && $team == ""){
        return false;
    }

    else {
    require './conectar.php';
    $resultset = $conn->prepare("SELECT * FROM users WHERE id = '$id' LIMIT 1");
    $resultset->execute();
    $resultkey = $resultset->fetch();

    if($resultkey !== false) {

        $update = "UPDATE users SET name = :name, password = '$password' WHERE id = '$id' LIMIT 1";
        $up = $conn->prepare($update);
        $up->bindParam(':name', $_POST['name'], FILTER_SANITIZE_SPECIAL_CHARS);
        $up->execute();
        $_SESSION['name'] = $up['name'];

    }

}

answered question

1 Answer

8

You need to first fetch the result of your query:

$up = $conn->prepare($update);
$up->bindParam(':name', $_POST['name'], FILTER_SANITIZE_SPECIAL_CHARS);
$up->execute();
$result = $up->fetch();
$_SESSION['name'] = $result['name'];

The fetch method in your framework might return an object instead of an array. In that case you can access the name property like this:

$result->name;

Either way, you can't use $up to get the name because, as your error says, it's an object of type PDOStatement and not actually the result of the query.

posted this

Have an answer?

JD

Please login first before posting an answer.