0

So I was wondering what if at the end of a simple SQL query in PHP like this:

    $sentence = $connection->prepare("
        SELECT * FROM posts_comments WHERE comment_on = $id ORDER BY date DESC LIMIT 2
    ");
    $sentence->execute();
    return $sentence->fetchAll();

I didn't put the fetchAll() function, so it would be like this:

    $sentence = $connection->prepare("
        SELECT * FROM posts_comments WHERE comment_on = $id ORDER BY date DESC LIMIT 2
    ");
    $sentence->execute();

Will there be any difference in the result or it would be the same?

  • 1
    If you don't fetch the results, you won't get the data from the DB. – Barmar Nov 28 '20 at 14:25
  • 1
    Side note: you're defeating the purpose of using prepared statements if you're directly inserting the parameter. Parametrize your query to avoid [SQL injection](https://stackoverflow.com/questions/332365/how-does-the-sql-injection-from-the-bobby-tables-xkcd-comic-work). – El_Vanja Nov 28 '20 at 14:26

0 Answers0