PHP do-while loop ending with exhausted memory error when executing sql code

173
July 05, 2017, at 00:05 AM

Whilst executing the following code, the site crashes with:

"Allowed memory size of 268435456 bytes exhausted (tried to allocate 254643 bytes)"

        $queID = "LCGQ00" . $ID;
        $sqlquecheck = "Select QueryID FROM Questions Where QueryID = '$queID' limit 1;";
        $quequery = $this->db->query($sqlquecheck);
        if($quequery->num_rows() > 0)
        {
            do{
                $queID = $queID . "-" . $ID;
                $quequery = $this->db->query($sqlquecheck);
            }while($quequery->num_rows() > 0);
        }

The error is pointing to the following line of code as the culprit:

$queID = $queID . "-" . $ID;

What I'm trying to do is pad an existing ID to create a new one, and if that already exist then pad it again and again and so on.

So if there is already a result, do the pad function then resend the query and check again if it still returns a result.

Any help would be greatly appreciated!

Edit FIXED:

        if($quequery->num_rows() > 0)
        {
            do{
                $queID = $queID . "-" . $ID;
                $sqlquecheck2 = "Select QueryID FROM Questions Where QueryID = '$queID' limit 1;";
                $quequery2 = $this->db->query($sqlquecheck2);
            }while($quequery2->num_rows() > 0);
        }

Also fetch_* and other methods were not working when I tried a different approach, which is why I used this method. This didn't need to be perfect, was just to eliminate a db error. Thanks again.

Answer 1

You're never changing $sqlquecheck, so it will run while Select QueryID FROM Questions Where QueryID = 'LCGQ00' limit 1;" returns something.

Unless some other process changes the database, it will run either 0 times or forever.

try

$queID = "LCGQ00" . $ID;
        $sqlquecheck = "Select QueryID FROM Questions Where QueryID = '$queID' limit 1;";
        $quequery = $this->db->query($sqlquecheck);
        if($quequery->num_rows() > 0)
        {
            do{
                $queID = $queID . "-" . $ID;
                $sqlquecheck = "Select QueryID FROM Questions Where QueryID = '$queID' limit 1;";
                $quequery = $this->db->query($sqlquecheck);
            }while($quequery->num_rows() > 0);
        }
READ ALSO
php search the value

php search the value

Hello every one i am new to php i need some help in search the exact value of school student roll numbers

121
How can I get data from my online mysql database to my pc so I can use it in my application?

How can I get data from my online mysql database to my pc so I can use it in my application?

I want to fetch the data from a web-based database into my application

179
Why is the wrong message shown?

Why is the wrong message shown?

I insert a row with following commands :

193
Show table from database after option selected from dropdown list

Show table from database after option selected from dropdown list

I need help to startI wan't to create a dropdown list with the values from table THEME and when you have selected a value from that list a table is shown with the rows that have the id of the selected value from the dropdown

170