John Davidson

php - Converting Query from forEach in a better solution

0 comments
Message:


foreach ($getComments as $comments) {
$comAuthor=$comments['uid'];

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

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);
$getUser->execute();
$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

Comments




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