Just learned PDO today and in the middle of making a twitter clone to learn using PHP/MySQL.
Here's a snippet of a code that is supposed to show all the tweets in the database under a certain user_id (kind of like the twitter feed).
<?php
$all_tweets = showtweets($pdo, $_SESSION['user_id']);
if (count($all_tweets)) {
foreach($all_tweets as $key => $value) {
?>
<div class="tweet">
<?php
echo $key['tweet'] . "<br>
Written By: " . $_SESSION['user_id'] . " on " . $key['time'] . "<br>";
?>
</div>
<?php
};
} else {
echo "You haven't posted anything yet!";
};
?>
Here's the function showtweets in my php code
function showtweets($pdo, $user_id) {
$show_tweets_stmt = "SELECT tweet, time FROM tweets WHERE id=:user_id
ORDER BY time DESC";
$show_tweets = $pdo->prepare($show_tweets_stmt);
$show_tweets->bindParam(':user_id', $_SESSION['user_id']);
$show_tweets_execute = $show_tweets->execute();
if (!$show_tweets_execute) {
printf("Tweets failed to be retrieved! <br> %s", $pdo->error);
} else {
$tweets_array = $show_tweets->fetchAll();
return $tweets_array;
};
}
I tried running isset($all_tweets)
and it returned true, while echoing count($all_tweets)
printed 0.
I think something is wrong with my addtweets
function code, and I've been dabbling with this all day but I can't get it to work.
Any advice? Much thanks