0

I am trying with this code to fetch all data but its not work.

my code for database connection

function initializeConnection(){
    $host       =   "localhost";
    $username   =   "xxx";
    $password   =   "xxx";
    $dbName     =   "xxx";
    $mysqli = new mysqli($host, $username, $password, $dbName);

    if (mysqli_connect_errno()) {
        die('Could not connect: ' . mysqli_connect_error());

    }
    return $mysqli;
}

my code for dfetch data

function getResultFetchAll($sql){
    global $mysqli;
    $mysqli = initializeConnection();
    $result = $mysqli->query($sql);
    return $mysqli->fetch_all(MYSQLI_ASSOC);
}

Error showing

PHP Fatal error:  Uncaught Error: Call to undefined method mysqli_result::fetch_all() 
  • 1
    `fetch_all()` is a method on `mysqli_result` (`$result`), not the `$mysqli` connection. So you want to `return $result->fetch_all(MYSQLI_ASSOC);` – Jeff Standen Oct 23 '17 at 19:25
  • You really shouldn't be doing this: You are just making it harder - if not impossible - to use a prepared statement and there is no benefit that I can think of. And opening a mysql connection per query... – jeroen Oct 23 '17 at 19:30

0 Answers0