7 Replies - 1739 Views - Last Post: 29 July 2014 - 10:30 AM Rate Topic: -----

#1 DarthJoker45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-July 14

Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 09:33 AM

Hello everyone,

I have ran into a bit of a rut. I apologize if this topic has been brought up before, but for some odd reason, one of my web pages is giving me an error. It is for a site where I need to register interns. Everything else in my code looks right, but I am getting this error. I cannot figure out what could be causing this. Here is a snapshot.

Posted Image

It is giving me the error on line 109. The problem is that the code should work. Here is my code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns= "http://www.w3.org/1999/xhtml">
<head>
<title>Available Opportunities</title>
<meta http-equiv="content-type"
     content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>College Internship</h1>
<h2>Available Opportunities</h2>
<?php
if (isset($_REQUEST['intern']))
       $InternID = $_REQUEST['internID'];  
else
     $InternID = -1;
if (isset($_COOKIE['LastRequestDate']))
        $LastRequestDate = $_COOKIE['LastRequestDate'];
else
    $LastRequestDate = "";

$errors = 0;
$DBConnect = @mysqli_connect("localhost", "root", "");
if ($DBConnect === FALSE) {
     echo "<p>Unable to connect to the database server. " .
	     "Error code " . mysqli_connect_errno() . ": " .
		 mysqli_connect_error() . "</p>\n";
     ++$errors;
}
else {
   $DBName = "internships";
   $result = @mysqli_select_db($DBConnect, $DBName);
   if ($result === FALSE) {
        echo "<p>Unable to select the database. " .
		   "Error code " . mysqli_errno($DBConnect)
		     . ": " .
			 mysqli_error($DBConnect) . "</p>\n";
		++$errors;
	}
}
$TableName = "interns";
if ($errors == 0) {
     $SQLstring = "SELECT * FROM $TableName WHERE " .
	       " internID = '$InternID'";
	$QueryResult = @mysqli_query($DBConnect, $SQLstring);
	if ($QueryResult === FALSE) {
	    echo "<p>Unable to execute the query. " .
		    "Error code " . mysqli_errno($DBConnect) 
			. ": " . mysqli_error($DBConnect) . "</p>\n";
		++$errors;
	}
	else {
	    if (mysqli_num_rows($QueryResult) == 0) {
		      echo "<p>Invalid Intern ID!</p>";
			  ++$errors;
	    }
	}
}
if ($errors == 0) {
    $Row = mysqli_fetch_assoc($QueryResult);
	$InternName = $Row['first'] . " " . $Row['last'];
}
else 
  $InternName = "";

$TableName = "assigned_opportunities";
$ApprovedOpportunities = 0;
$SQLstring = "SELECT COUNT(opportunityID) FROM
  $TableName " .
     " WHERE internID='$InternID' " .
	 " AND date_approved IS NOT NULL";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
if (mysqli_num_rows($QueryResult) > 0) {
     $Row = mysqli_fetch_row($QueryResult);
	 $ApprovedOpprtunities = $Row[0];
	 mysqli_free_result($QueryResult);
}
$SelectedOpportunities = array();
$SQLstring = "SELECT opportunityID FROM $TableName " .
   " WHERE internID= ' $InternID'";
$QueryResult = @mysqli_query($DBConnect, $SQLstring);
if (mysqli_num_rows($QueryResult) > 0) {
    while (($Row = mysqli_fetch_row($QueryResult))
	  !== FALSE)
	  $SelectedOpportunities[] = $Row[0];
	  mysqli_free_result($QueryResult);
}
$AssignedOpportunities = array();
$SQLstring = "SELECT opportunityID FROM $TableName " .
      " WHERE date_approved IS NOT NULL";
$QueryResult = mysqli_query($DBConnect, $SQLstring);
if (mysqli_num_rows($QueryResult) > 0) {
    while (($Row = mysqli_fetch_row($QueryResult))
	 !== FALSE)
	   $AssignedOpportunities[] = $Row[0];
	 mysqli_free_result($QueryResult);
}
if (!empty($LastRequestDate))
      echo "<p>You last requested an internship opportunity " .
	    " on $LastRequestDate.</p>\n";
$TableName = "opportunities";
$Opportunities = array();
$SQLstring = "SELECT opportunityID, company, city, " .
    " start_date, end_date, position, description " .
	" FROM $TableName";
$QueryResult = mysqli_query($DBConnect, $SQLstring);
if (mysqli_num_rows($QueryResult) > 0) {
    while (($Row = mysqli_fetch_assoc($QueryResult)) !== FALSE)
	      $Opportunities[] = $Row;
		  mysqli_free_result($QueryResult);
}
mysqli_close($DBConnect);
echo "<table border='1' width='100%'>\n";
echo "<tr>\n";
echo "     <th style='background-color:cyan'>Company</th>\n";
echo "     <th style='background-color:cyan'>City</th>\n";
echo "     <th style='background-color:cyan'>End Date</th>\n";
echo "     <th style='background-color:cyan'>Position</th>\n";
echo "     <th style='background-color:cyan'>Description</th>\n";
echo "     <th style='background-color:cyan'>Status</th>\n";
echo "</tr>\n";
foreach ($Opportunities as $Opportunity) {
  if (!in_array($Opportunity['opportunityID'], 
     $AssignedOpportunities)) {
	echo "<tr>\n";
	echo "    <td>" .
	     htmlentities($Opportunity['company']) .
		 "</td>\n";
	echo "    <td>"  .
	     htmlentities($Opportunity['city']) .
		   "</td>\n";
	echo "    <td>"  . 
	   htmlentities($Opportunity['start_date']) .
	        "</td>\n";
	echo "    <td>"  .
	     htmlentities($Opportunity['end_date']) .
		   "</td>\n";	
	echo "    <td>"  .
	     htmlentities($Opportunity['position']) .
		   "</td>\n";	
    echo "    <td>"  .
	     htmlentities($Opportunity['description']) .
		   "</td>\n";		 
    echo "    <td>";
    if (in_array($Opportunity['opportunityID'],
         $SelectedOpportunities))
         echo "Selected";
    else {
        if ($ApprovedOpportunities>0)
           echo "Open";
        else
           echo "<a href= 'Request.Opportunity.php?" .
                  "internID=$InternID&" .
                  "opportunityID =" .
                 $Opportunity['opportunityID'] .
                 "'>Available</a>";
    }
    echo "</td>\n";
    echo "</tr>\n";
  }
}
echo "</table>\n";
echo "<p><a href='InternLogin.php'>Log Out</a></p>\n";  
?>
</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: Fatal error: Allowed memory size of 134217728 bytes exhausted

#2 chris98   User is offline

  • D.I.C Lover

Reputation: 43
  • View blog
  • Posts: 1,155
  • Joined: 06-July 13

Re: Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 09:47 AM

I got this error today, and I found it due to my while loop. You could change it to a foreach() for example, and it should work.

You can also add this to remove the limitation:

ini_set('memory_limit', '-1');

This post has been edited by chris98: 29 July 2014 - 09:48 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • palpable absurdity
  • member icon

Reputation: 6918
  • View blog
  • Posts: 28,600
  • Joined: 12-December 12

Re: Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 09:47 AM

You should take line 111 out of the loop (unless you are only expecting a single row), or try just commenting it out initially.
Was This Post Helpful? 0
  • +
  • -

#4 DarthJoker45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-July 14

Re: Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 09:50 AM

View Postchris98, on 29 July 2014 - 04:47 PM, said:

I got this error today, and I found it due to my while loop. You could change it to a foreach() for example, and it should work.

You can also add this to remove the limitation:

ini_set('memory_limit', '-1');


Where exactly would that code be placed in the file?
Was This Post Helpful? 0
  • +
  • -

#5 chris98   User is offline

  • D.I.C Lover

Reputation: 43
  • View blog
  • Posts: 1,155
  • Joined: 06-July 13

Re: Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 09:54 AM

At the top after the php tags. But do note that if it is due to your while loop, then it will probably (I'm about 80% certain) result in a redirect loop so it won't accomplish anything. Your best approach would probably be a foreach instead.

Also, don't just insert the data into your queries as you are highly susceptible to sql injection. There is no point in using MySQLi if you're going to do that, as it does nothing without prepared statements that MySQL doesn't. You must use prepared statements!

See how your code is vulnerable by seeing this tutorial on SQL Injection - attempt it on your script. Only then you'll understand how vulnerable it is.

This post has been edited by chris98: 29 July 2014 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 chris98   User is offline

  • D.I.C Lover

Reputation: 43
  • View blog
  • Posts: 1,155
  • Joined: 06-July 13

Re: Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 09:59 AM

Also, don't use $_REQUEST. Use $_GET or $_POST as needed.
Was This Post Helpful? 0
  • +
  • -

#7 DarthJoker45   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-July 14

Re: Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 10:26 AM

View Postchris98, on 29 July 2014 - 04:54 PM, said:

At the top after the php tags. But do note that if it is due to your while loop, then it will probably (I'm about 80% certain) result in a redirect loop so it won't accomplish anything. Your best approach would probably be a foreach instead.

Also, don't just insert the data into your queries as you are highly susceptible to sql injection. There is no point in using MySQLi if you're going to do that, as it does nothing without prepared statements that MySQL doesn't. You must use prepared statements!

See how your code is vulnerable by seeing this tutorial on SQL Injection - attempt it on your script. Only then you'll understand how vulnerable it is.

Well, here is the problem. I got the code straight out of a text book. I should have told you guys before hand. It is for a homework assignment. The code is straight out of our textbook. The author said that this code has been intentionally typed out like this just so the reader understands the concepts of managing state information. He acknowledges that the code would have security issues. Do not think that I am cheating. I had everything typed out before I came on here. I had to use mysqli because my version of the PHP server does not support mysql statements but only mysqli. I just cannot understand why it would perform this error.
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2814
  • View blog
  • Posts: 8,166
  • Joined: 15-January 14

Re: Fatal error: Allowed memory size of 134217728 bytes exhausted

Posted 29 July 2014 - 10:30 AM

The problem is that your code is using too much memory. Changing a while loop to a foreach loop isn't going to do anything to help that (and, in this case, won't work anyway with fetching database results). If you are using more than 128MB of memory, then either your script is just getting a ton of data or you have an infinite loop somewhere. Instead of just removing the memory limit, let's look into why your script is using so much memory.

You can use this function to show how much memory is currently allocated:

http://www.php.net/m...y-get-usage.php

Add some statements to print the memory usage after various parts of your code. For example, after the loops on lines 78, 88, 98, 112, etc, add statements to print the memory usage. That will help you identify where all the memory is going.

Your final query is getting every record from the opportunities table, is it really necessary to get all of that data? When you're looping through those records you have an if statement where you only display certain records, why not only get the records you are going to display instead of all of them?

Here's another thought - when you run a query and return the result resource, the results are buffered, it does not store everything in memory yet. They are buffered on the database server. But when you loop through them and add everything to an array, you are unbuffering the results and using up memory for each result. Instead of looping through the result set and adding everything to an array, and then later looping through the array, it would be much more efficient to skip the array and just loop through the result set to display each record.

This post has been edited by ArtificialSoldier: 29 July 2014 - 10:31 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1