By seyeredg


2020-06-30 03:44:51 8 Comments

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

    }

}

1 comments

@zeterain 2020-06-30 03:50:13

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.

@seyeredg 2020-06-30 15:09:21

thank you so much, now I understand it.

@zeterain 2020-06-30 16:12:02

You're welcome! If this answer solved your problem, consider marking it as accepted to help future users identify a correct answer.

Related Questions

Sponsored Content

38 Answered Questions

[SOLVED] Deleting an element from an array in PHP

  • 2008-12-15 20:28:55
  • Ben
  • 2665477 View
  • 2543 Score
  • 38 Answer
  • Tags:   php arrays unset

18 Answered Questions

[SOLVED] Sort array of objects by object fields

23 Answered Questions

[SOLVED] PHP array delete by value (not key)

  • 2011-08-29 00:44:04
  • Adam Strudwick
  • 851013 View
  • 895 Score
  • 23 Answer
  • Tags:   php arrays

11 Answered Questions

[SOLVED] How to Sort Multi-dimensional Array by Value?

15 Answered Questions

[SOLVED] Cannot use object of type stdClass as array?

  • 2011-07-25 11:40:14
  • Dail
  • 650199 View
  • 543 Score
  • 15 Answer
  • Tags:   php json

39 Answered Questions

[SOLVED] Get the first element of an array

  • 2009-12-17 12:26:44
  • hsz
  • 1437038 View
  • 1081 Score
  • 39 Answer
  • Tags:   php arrays

31 Answered Questions

[SOLVED] Convert a PHP object to an associative array

  • 2010-12-03 12:34:16
  • Haroldo
  • 1175129 View
  • 767 Score
  • 31 Answer
  • Tags:   php arrays

13 Answered Questions

[SOLVED] PHPDoc type hinting for array of objects?

  • 2009-04-22 18:29:13
  • Artem Russakovskii
  • 186942 View
  • 417 Score
  • 13 Answer
  • Tags:   php ide phpdoc var hint

1 Answered Questions

Sponsored Content