0

hoping someone can help me, I am having the following error, looked online and tried a load of things but can't seem to figure it out, error:

Fatal error: Call to undefined method mysqli::mysqli_fetch_all() in C:\xampp\htdocs\cyberglide\core-class.php on line 38

heres my code:

<?php

class Core {

function db_connect() {
    global $db_username;
    global $db_password;
    global $db_host;
    global $db_database;

    static $conn;

    $conn = new mysqli($db_host, $db_username, $db_password, $db_database);

    if ($conn->connect_error) {
        return '<h1>'."Opps there seems to be a problem!".'</h1>'.'<p>'."Your Config file is not setup correctly.".'</p>';
    }
    return $conn;
}

function db_content() {
    //this requires a get, update and delete sections, before its complete

    $conn = $this->db_connect();

    if(mysqli_connect_errno()){
        echo mysqli_connect_error();
}
$query = "SELECT * FROM content";

// Escape Query
$query = $conn->real_escape_string($query);

// Execute Query
if($result = $conn->query($query)){

        // Cycle through results
        while($row = $conn->mysqli_fetch_all()){
            //echo $row->column;
           }
       }
   }
}

$core = new Core();

?>

I am trying to create a db_connect function, which I want to be able to call anywhere on the site that needs a database connection, I am trying to call that function on a function within the same class, I want it to grab and display the results from the database. I am running PHP 5.4.7, I am calling the database on a blank php file which includes a require to include the class file, then using this at the moment $core->db_content(); to test the function. I am building this application from scratch, running from MySQLi guides (not used MySQLi before, used to use normal MySQL query's) so if I am doing anything wrong please let me know, thanks everyone.

4
  • What version of PHP are you on? mysqli_fetch_all PHP 5 >= 5.3.0 Commented Jan 20, 2014 at 22:35
  • Where are you calling db_content()? Commented Jan 20, 2014 at 22:37
  • existence of method got nothing to do with method order. Commented Jan 20, 2014 at 22:38
  • 1
    You shouldn't escape a complete query, that's escaping done the wrong way! Commented Jan 20, 2014 at 22:41

4 Answers 4

5

mysqli_fetch_all is a method of a mysqli_result, not mysqli.

So presumably it should be $result->fetch_all()

References:

Important: keep in mind mysqli_result::fetch_all returns the whole result set not a row as you assume in your code

Sign up to request clarification or add additional context in comments.

Comments

4

There are three problems I see here.

while($row = $conn->mysqli_fetch_all()){
  1. The method name is fetch_all() when used in the OOP way.
  2. fetch_all() should be used with the $result object
  3. fetch_all() is only available when the mysqlnd driver is installed - it frequently is not.

Reference

Comments

1

Only $result has that method. If you want to use it in a while loop use fetch_assoc(). fetch_all() returns an associative array with all the data already.

while($row = $result->fetch_assoc()){

}

Comments

-1

thanks all, its working fine now, i had it as while($row = $conn->fetch_assoc()){ } before and changed to what i put above, but dident see it should of been $result instead of $conn, thanks for pointing that out.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.