0

I'm trying to echo out every single thing in a table from sql, my code is as follows

$stmt = $link->prepare("SELECT * FROM articles");
$stmt->execute();
$stmt->store_result();
if (mysqli_stmt_num_rows($stmt) >= 1) {
  $result = mysqli_stmt_get_result($stmt); //get result object
  while ($row = mysqli_fetch_assoc($result)){ //get associative array
      $news = $row['title'];
  }
}

It doesn't work, returning as mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given

I've done my research but literally nothing works :(

1 Answers1

0

You don't need to use prepared statements for this SELECT query as you aren't specifying anything WHERE.

$query = 'SELECT * FROM articles';

if ($result = $link->query($query)) {
    while ($row = $result->fetch_assoc()) {
        $news = $row['title'];
    }
}

For a really good in-depth answer check this out: https://stackoverflow.com/a/11575617/1427345

Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
  • Thank you so much! On a side note how do I echo out each result? I've added `LIMIT 0,25` in the query to limit the search already. So far, i'm trying to use `foreach ($row as $row['content'])` as a method of recursion but i have no luck so far, it only echos out the newest data in db. When its `foreach ($result as $row['content'])`, I get `Notice: Array to string conversion` whereby it can't echo out the "content" array ---- Nevermind, I found out how, thanks! –  May 17 '18 at 12:43