Saturday, 17 August 2013

Fatal error: Allowed memory size of 134217728 bytes exhausted, improve the code

Fatal error: Allowed memory size of 134217728 bytes exhausted, improve the
code

I know that this question It's been answered several times, but i don't
want to extend my memory usage, so if you can help me i will be very
grateful, then to the point:
I have the next code:
<?php
function add($var,$str_rep,$position)
{
if (!is_numeric($position) && ((strcasecmp($position,"beginning")==0) ||
(strcasecmp($position,"end")==0)))
{
$position=(strcasecmp($position,"beginning")==0) ?
0:(strcasecmp($position,"end")==0)? strlen($var):$position;
$string=substr_replace($var,$str_rep,$position,0);
return $string;
}
else
{
return false;
}
}
include 'cnn.php';
if (isset($_POST["page"]) && isset($_POST["table"]))
{
$query1=$cnn->query("describe bebidas.".$_POST["table"]."");
/*Get the table fields*/
$nums_fields=$query1->num_rows;
$start_num=($_POST["page"]-1)*6;
$query2=$cnn->query("select * from bebidas.".$_POST["table"]."");
/*Get the number of rows*/
$num=$query2->num_rows;
mysqli_free_result($query2);
$pages=ceil($num/6);
$rTables=array();
$fK=array();
$pk=array();
if ($num >0)
{
$query2=$cnn->query('select REFERENCED_TABLE_NAME as tabla
,COLUMN_NAME as fKey,REFERENCED_COLUMN_NAME as pKey from
information_schema.KEY_COLUMN_USAGE where
TABLE_NAME="'.$_POST['table'].'" AND REFERENCED_TABLE_NAME IS NOT NULL
AND REFERENCED_COLUMN_NAME IS NOT NULL');
//Query that give me the metadata of table-related fields
while($table=$query2->fetch_object())
{
$rTables[]=$table->tabla;
$fK[]=$table->fKey;
$pK[]=$table->pKey;
}
mysqli_free_result($query2);
$flag=count($rTables);
$FieldName=array();
if ($flag>0)
{
$consulta="select COLUMN_NAME as FieldName from
information_schema.COLUMNS where TABLE_SCHEMA='bebidas' and
TABLE_NAME in (";
$i=0;
while ($flag>$i)
{
$consulta.="".$rTables[$i].",";
$i++;
}
$consulta.=") and ORDINAL_POSITION=2";
$query2=$cnn->query($consulta);
while ($field=$query2->fetch_object())
{
$FieldName[]=$field->FieldName;
}
}
//Query that give me the fields that I really want
$ForeignKeys=array();
$msg="<table><thead><tr><th><input type='checkbox'
id='table-select-all'></th>";
while ($row=$query1->fetch_object())
{
if ($row->Key=="MUL")
{
for ($i=0;$i<count($fK);$i++)
{
if (($row->Field)==($fk[$i]))
{
$campo=$FieldName[$i];
}
}
$msg.="<th>".$campo."</th>";
$ForeignKeys[]=$row->Field;
}
else
{
$msg.="<th>".$row->Field."</th>";
$FieldName[]=$row->Field;
}
if ($row->Key=="PRI")
{
$pKeyTable=$row->Field;
}
}
//Show the NameFields
mysqli_free_result($query1);
$msg.="</tr></thead><tfoot>";
//Make the query that will give me the information of the table and its
related fields
$consulta="select ";
for ($i=0;count($FieldName)>$i;$i++)
{
$consulta.="".$FieldName[$i].",";
}
$i=0;
$flag=count($ForeignKeys);
if ($flag>0)
{
while ($flag>$i)
{
$consulta.="".$ForeignKeys[$i].",";
$i++;
}
}
$consulta=trim($consulta,',');
$consulta.=" from bebidas.".$_POST["table"]." T1";
$i=0;
while (isset($rTables[$i]))
{
$numTb=$i+2;
$consulta.=" inner join".$rTables[$i]." $numTb on
T1.".$fKey[$i]."=".$numTb.".".$pKey[$i]."";
$i++;
}
$consulta.=" order by $pKeyTable DESC limit $start_num,6";
$query=$cnn->query($consulta);
echo $consulta;
$num=$query->num_rows;
if ($pages >1)
{
$msg.="<tr><td colspan='".$nums_fields."' class='table-footer
pagination-list'><ul>";
//Make the pagination
$i=$_POST["page"];
$flag=$_POST["page"]+5;
while ($i<=$flag)
{
if ($_POST["page"]==$i)
{
$list="<li><a href='#' id='".$i."'
class='active'><".$i."</a></li>";
}
else
{
$list="<li><a href='#' id='".$i."'><".$i."</a></li>";
}
$i++;
}
if ($_POST["page"]==1)
{
$str_rep="<li><a href='#' id='2'
title='Siguiente'>&rsaquo;</a></li><li><a href='#'
id='".$pages."' title='Ultimo'>&raquo;</a></li>";
$list=add($list,$str_rep,"end");
}
elseif ($_POST["page"]==$pages)
{
$str_rep="<li><a href='#' id='1'
title='Primero'>&laquo;</a></li><li><a href='#'
id='".($pages-1)."' title='Anterior'>&lsaquo;</a></li>";
$list=add($list,$str_rep,"beginning");
}
else
{
$str_rep="<li><a href='#' id='".($_POST["page"]+1)."'
title='Siguiente'>&rsaquo;</a></li><li><a href='#'
id='".$pages."' title='Ultimo'>&raquo;</a></li>";
$list=add($list,$str_rep,"end");
$str_rep="<li><a href='#' id='1'
title='Primero'>&laquo;</a></li><li><a href='#'
id='".($_POST["page"]-1)."'
title='Anterior'>&lsaquo;</a></li>";
$list=add($list,$str_rep,"beginning");
}
$msg=$msg.$list;
$msg.="</ul></td></tr>";
}
$msg.="<tr><td colspan='".$nums_fields."' class='table-footer'><label
for='table-select-actions'>With selected:</label><select
id='table-select-actions'><option
value='option1'>Delete</option><option
value='option2'>Export</option><option
value='option3'>Archive</option></select><a href='#'' class='round
button blue text-upper small-button'>Apply to
selected</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a
href='#'' class='button round blue image-right ic-add
text-upper'>Agregar Registro</a></td></tr></tfoot><tbody>";
$i=0;
$pKeyTable=(isset($pKeyTable)) ? $pKeyTable : $ForeignKeys;
//Show the table's data
while ($row=$query->fetch_assoc())
{
$value=(!is_array($pKeyTable)) ? $pKeyTable :
implode(",",array_intersect_key($row,array_flip($pKeyTable)));
$msg.="<tr><td><input type='checkbox' name='borrar[]''
value='".$value."'></td>";
while (isset($FieldName[$i]))
{
$Field=$FieldName[$i];
$msg.="<td>".$row[$Field]."</td>";/*Here the matter*/
}
$msg.="<td><a href='#' class='table-actions-button ic-table-edit'
id='".$value."'></a><a href='#' class='table-actions-button
ic-table-delete' id='".$value."'></a></td></tr>";
}
$msg.="</tbody></table>";
}
else
{
$msg="<center><h1>No hay ningun dato que mostrar</h1></center>";
}
//Show everything
echo $msg;
}
but when i try to test the code, I always get the same error:
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to
allocate 133693397 bytes) in
/var/www/SistemaBebidas/private/ShowRecord.php on line 161, but i don't
understand why in that code's line.
So i wanna know in what way i can improve this code?

No comments:

Post a Comment