Jump to content

Array to string conversion error when retrieving data from DB(MySQL)


Dorothy17

Recommended Posts

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

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

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

$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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.