John Davidson

php - Converting Query from forEach in a better solution


foreach ($getComments as $comments) {

$getUser = $pdo->query("SELECT * FROM user WHERE uid = $comAuthor")->fetch();
$user = $getUser['name'];

echo "<div class='comment'><a href='profile?uid=$comAuthor'>$user</a><p>$comContent</p><likes>$comLikes</likes></div>";

I have been trying to get this query to work in a forEach cycle, but after I have done my research I found out it's impossible to run it more than once. I need help trying to get this to work. I am new to PHP and a lot of stuff is unfamiliar to me.

My Answer:
>prepare("SELECT * FROM users WHERE id = :id");
$getUser->bindParam(':id', $comAuthor);
$user = $getUser->fetch(PDO::FETCH_ASSOC);

echo "Comment Author: " . $user['username'] . "
echo "Comment: " . $comments['comment'] . "

Rate this post

5 of 5 based on 1771 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us