Jump to content

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


Go to solution Solved by Psycho,

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.

Edited by requinix

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.

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.

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.

  • Solution
$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);
Edited by Psycho
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.