简体   繁体   中英

Cannot use object of type PDOStatement as array in

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'];

    }

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM