Palash Gupta
Palash Gupta

Reputation: 118

Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes)

can anyone help me on this error!
"Allowed memory size of 134217728 bytes exhausted (tried to allocate 36 bytes)"

this error occurs when i fetch record from database table of all courses available (total course are 84 in number).
My function is:-

function allCouses()
{
    include 'connect.php';
    $qry = "SELECT * FROM master_course WHERE mc_enabled=1 AND mc_deleted=0";
    $data = mysqli_query($con, $qry);
    if(mysqli_num_rows($data))
    {
        $record = mysqli_fetch_assoc($data);
        while($record)
            $course[]=$record;
        return $course;
    }
}

the error occur When i store all the record in $course[].

Upvotes: 0

Views: 1748

Answers (1)

RiggsFolly
RiggsFolly

Reputation: 94682

You are looping on, well the wrong thing, and have written an infinite while loop, and you need the mysqli_fetch_assoc inside the while loop to read all the rows in the result set.

function allCouses()
{
    include 'connect.php';
    $qry = "SELECT * FROM master_course WHERE mc_enabled=1 AND mc_deleted=0";
    $data = mysqli_query($con, $qry);

    while ($record = mysqli_fetch_assoc($data) ) {
        $course[]=$record;
    }
    return $course;
}

It would also be a better idea to make your database connection in the main body of the script and then pass $con as a parameter to each function that requires it. The process of making a connection is in comparison quite a slow one, so you only want to do it once per script.

<?php
function allCouses($con)
{
    $qry = "SELECT * FROM master_course WHERE mc_enabled=1 AND mc_deleted=0";
    $data = mysqli_query($con, $qry);

    while ($record = mysqli_fetch_assoc($data) ) {
        $course[]=$record;
    }
    return $course;
}



include 'connect.php';

$courses = allCourses($con);

Upvotes: 2

Related Questions