0

This file is names as public-function.php

function getPublishedPosts() {
// use global $conn object in function
global $conn;
$sql = "SELECT * FROM posts WHERE published=true";
$result = mysqli_query($conn, $sql);

// fetch all posts as an associative array called $posts
$posts = mysqli_fetch_all($result, MYSQLI_ASSOC);
return $posts;
}

and while calling data to index.php with command

<?php require_once( ROOT_PATH . '/includes/public_functions.php') ?>

<!-- Retrieve all posts from database  -->
<?php $posts = getPublishedPosts(); ?>

Its showing the error as:

Fatal error: Uncaught Error: Call to undefined function mysqli_fetch_all() in C:\MAMP\htdocs\complete-blog-php\includes\public_functions.php:12 Stack trace: #0 C:\MAMP\htdocs\complete-blog-php\index.php(10): getPublishedPosts() #1 {main} thrown in C:\MAMP\htdocs\complete-blog-php\includes\public_functions.php on line 12

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Ankit Shah
  • 1,087
  • 2
  • 15
  • 30
  • The error is about a function called `mysqli_fetch` which isn't in the source code for `getPublishedPosts()` you have here. – Nigel Ren Nov 01 '18 at 06:55
  • 1
    As said in [manual](http://php.net/manual/en/mysqli-result.fetch-all.php#refsect1-mysqli-result.fetch-all-mysqlnd): `mysqli_fetch_all` is "Available only with mysqlnd." – u_mulder Nov 01 '18 at 06:56
  • What should I write so that the programs axecutes as same as it would in mysqlnd – Ankit Shah Nov 01 '18 at 07:06
  • Something like this `$result = $mysqli->query($sql); $allData = []; while ($item = $result->fetch_assoc()) { $allData[] = $item; } print_r($allData);` – Nathan Meyer Nov 01 '18 at 09:42

0 Answers0