0

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

icedwater
  • 4,701
  • 3
  • 35
  • 50
Python Learner
  • 185
  • 1
  • 12
  • `isset` returning TRUE does not mean the variable is not empty. Noticed that you passed `$user_id` into `showtweets()`, but used `$_SESSION['user_id']`, is this expected? – Passerby Jul 16 '13 at 03:02
  • @Passerby Thanks for clearing that up! And yes that was expected, I don't think I could pass superglobals as variablese in the function. At least it gave me a fatal error when I did. – Python Learner Jul 16 '13 at 03:05
  • Would you happen to know why my array is turning up empty? – Python Learner Jul 16 '13 at 03:06
  • Use `var_dump` on variables to check if everything is going as expected. Go to MySQL terminal and run the same query to see if the result is actually not empty. Your code does not seem to have obvious error. – Passerby Jul 16 '13 at 03:08
  • @Passerby WOW dumb mistake. was supposed to pass user_id=:user_id in my statement instead if id=user_id. – Python Learner Jul 16 '13 at 03:15

2 Answers2

1

From PHP, isset:

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

So, in your case, the variable $all_tweets has been set, and the value is not NULL.

More specifically, your issue lies in how you determine if an array is empty to begin with. To determine if $all_tweets is empty in PHP - the internet, and (mainly) a lot of StackOverflow posts strongly suggest using empty(), which (from the PHP docs):

Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.

The following things are considered to be empty:

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Also, for more reading on the matter, examine this: How to check whether an array is empty using PHP?

Community
  • 1
  • 1
jrd1
  • 10,358
  • 4
  • 34
  • 51
0

If you want to see if an array is empty or not use empty($array) isset($array) returns true because there is an empty array exist.

DevZer0
  • 13,433
  • 7
  • 27
  • 51