Dorothy17 Posted April 6, 2017 Share Posted April 6, 2017 Hello, I have an issue with an SQL statement (according to the error message the execute() line) and I cannot figure out where I go wrong. I created a page, where a user can fill out a test, it's working and I can store the results successfully in the DB. BUT I'd like to retrieve these results on the user's profile page and then I get the above error. I tried to use fetch() instead of fetchAll(), but the error is the same, and fetchAll() should be fine, I think. I use this function for sql query: public function showTestResults($userID) { $connection = DatabaseConnection::getConnection(); $statement = $connection->prepare("SELECT * FROM useranswers WHERE userID=?"); $statement->execute([$userID]); return $statement->fetchAll(); } My function for the profile page: public function myProfileAction() { $isLoggedIn = $this->isLoggedInFromSession(); $email = $this->emailFromSession(); $user=$this->usersRepository->getUserId($email); $testResults=$this->testsRepository->showTestResults($user); require_once __DIR__ . '/../templates/myProfile.php'; } And the page itself: <h1>My Profile</h1> <p>Your registered email: <?= $user['email'] ?></p> <p>Your test results:</p> <?php foreach ($testResults as $testResult): ?> <p>Test <?= $testResult['testID'] ?> - Score: 10/<?= $testResult['score'] ?> - on: <?= $testResult['timeStamp'] ?></p> <?php endforeach; ?> Could you, please, help me what I'm missing here? Thanks a lot for your help in advance. Link to comment Share on other sites More sharing options...
requinix Posted April 6, 2017 Share Posted April 6, 2017 Please don't paste your code with your editor's syntax highlighting. It works well when writing code but it doesn't work well with forums. Paste your code as plain text, then put [code][/code] tags around each piece to use our own highlighting system. I've done it for you this time (and the time before) but please do it yourself in the future. Exactly which line is the error message pointing out? It means one of the variables on whichever line is an array when you are not expecting it to be. Link to comment Share on other sites More sharing options...
NigelRel3 Posted April 6, 2017 Share Posted April 6, 2017 What library are you using for the database access? You use DatabaseConnection::getConnection() to get the connection, so this should be defined somewhere. This will affect how the prepare and execute methods are called. Link to comment Share on other sites More sharing options...
Dorothy17 Posted April 6, 2017 Author Share Posted April 6, 2017 Sorry, will put the code into those tags next time. The error points to the $statement-> execute([userID]). I used the same concept with different data and it worked fine, so that's why I'm not sure where I go wrong. The database access is a separate class, that should be fine, I think, because the other functions and sql quires work fine. Link to comment Share on other sites More sharing options...
Psycho Posted April 6, 2017 Share Posted April 6, 2017 $statement->execute([$userID]); What IS the variable $userID? If it is already an array, then putting it in [] (brackets) would make it a multi-dimensional array (and possibly cause an array to string conversion error) What does this output? var_dump([$userID); Link to comment Share on other sites More sharing options...
Dorothy17 Posted April 7, 2017 Author Share Posted April 7, 2017 Thank you for your help, managed to solve it! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.