Fetching in while loop from database oops

hi

i am trying to fetch all my data from table

here is the code


function premiumview($cn,$table)
	{
		$table=mysqli_real_escape_string($cn,$table);
		$result=$cn->query("select * from $table");
		if($result->num_rows >0)
		{
			while($f=$result->fetch_all(MYSQL_ASSOC))
			{
				$result[]=$f;
			}
			return $result;
		}
		else
		{
			return $result=$cn->error;
		}
	}

and the error is

Fatal error: Cannot use object of type mysqli_result as array ( in while loop line )

What i am doing wrong?

You are using $result for two different purposes at the same time. Since it can’t be both a database pointer and an array to hold the results at the same time you need to rename the variable you use for one of these. For example:

function premiumview($cn,$table) 
    { 
        $table=mysqli_real_escape_string($cn,$table); 
        $dbresult=$cn->query("select * from $table"); 
        if($dbresult->num_rows >0) 
        { 
            while($f=$dbresult->fetch_all(MYSQL_ASSOC)) 
            { 
                $result[]=$f; 
            } 
            return $result; 
        } 
        else 
        { 
            return $result=$cn->error; 
        } 
    } 

Thanks :slight_smile: your answer works too… i also find a solution without while loop
here it is

if($result->num_rows >0)
{
$f=$result->fetch_all();
return $f;
}