I have just added a new query
to my webpage that uses a while loop
to produce an array
of the results. However when I try to run the page I get the error: "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) on line 305"
. I cannot work out why this has suddenly occurred, or what the memory leak might be. I do not wish to increase the size of the PHP memory_limit
.
My question is different... I am asking for advice on what might be causing the memory leak, not what this fatal error is or means.
Query in question:
<?php
$result1 = $con->query("SELECT SkillID FROM userskills WHERE UserID = '$User'") or die(mysqli_error($con));
$current_skills = array();
while (($skillrow = mysqli_fetch_array($result1, MYSQLI_NUM)) !== false){
$current_skills[] = $skillrow;
}
?>
Full Page:
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
require 'Assets/Connections/Connections.php';
session_start();
if(isset($_SESSION["UserID"]))
{
}
else
{
header('Location: LogIn.php');
die();
}
$User = (int)$_SESSION["UserID"];
$result = $con->query("SELECT * FROM user WHERE UserID ='$User'") or die(mysqli_error($con));
$row = $result->fetch_array(MYSQLI_BOTH);
$_SESSION["FirstName"] = $row['Fname'];
$_SESSION["LastName"] = $row['Lname'];
$_SESSION["Email"] = $row['Email'];
$_SESSION["Role"] = $row['JobRole'];
$skillresult = $con->query("SELECT userskills.SkillID, Description, Experience FROM User INNER JOIN userskills ON User.UserId = userskills.UserId JOIN Skills ON userskills.SkillID = Skills.SkillID WHERE user.UserID ='$User'")
or die(mysqli_error($con));
$skills_array = array();
while($r=mysqli_fetch_array($skillresult)){
if (!isset($skills_array[$r['SkillID']])){
$skills_array[$r['SkillID']] = array();
}
$skills_array[$r['SkillID']][] = $r['Description'];
}
if(isset($_POST['Update']))
{
$UpdateFName = $_SESSION["FirstName"];
if ($_POST['FirstName'] != '' ) { $UpdateFName = $_POST['FirstName'];}
$UpdateLName = $_SESSION["LastName"];
if ($_POST['LastName'] != '' ) { $UpdateLName = $_POST['LastName'];}
$UpdateEmail = $_SESSION["Email"];
if ($_POST['Email'] != '' ) { $UpdateEmail = $_POST['Email'];}
$UpdateRole = $_SESSION["Role"];
if ($_POST['JobRole'] != '' ) { $UpdateRole = $_POST['JobRole'];}
$PasswordCheck = $_POST['Password'];
if(password_verify($PasswordCheck, $row['Password']))
{
$sql = $con->query("UPDATE user SET
Fname = '{$UpdateFName}',
Lname = '{$UpdateLName}',
Email = '{$UpdateEmail}',
JobRole = '{$UpdateRole}'
WHERE UserID = $User") or die(mysqli_error($con));
if(!empty($_FILES['file']['name']))
{
$file = basename($_FILES['file']['name']);
move_uploaded_file($_FILES['file']['tmp_name'], 'Assets/Images/'.$file);
}
if(isset($file))
{
$sql = $con->query("UPDATE user SET ProfileImage = '".$_FILES['file']['name']."' WHERE UserID = $User") or die(mysqli_error($con));
}
$default = 0;
foreach($skills_array AS $skills_id=>$skills_name)
{
if (isset($_POST[$skills_name]))
{
if (empty($_POST[$skills_name.'exp']))
{
$exp = $default;
}
else
{
$exp = $_POST[$skills_name.'exp'];
}
$sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = ".$skills_id) or die(mysqli_error($con));
if ($row = mysqli_fetch_assoc($sql))
{
$sql = $con->query("INSERT INTO userskills ( UserID, SkillID, Experience) VALUES ($User, $skills_id, $exp)");
//If the checkbox is not checked it will check to see if skill is already a skill assigned to the user. If they are it will delete it. If not it will ignore.
}
else
{
$sql = $con->query("UPDATE userskills SET Experience = $exp WHERE UserID = $User AND SkillID = ".$skills_id);
}
}
else
{
$sql = $con->query("DELETE FROM userskills WHERE UserID = $User AND SkillID = ".$skills_id);
}
}
header('Location: Account.php');
die();
}
else
{
echo 'Incorrect password please try again.';
}
}
?>
<!doctype html>
<html>
<head>
<link href="Assets/CSS/Master.css" rel="stylesheet" type="text/css" />
<link href="Assets/CSS/Menu.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Update Account</title>
</head>
<body>
<div class="Container">
<div class="Header"></div>
<div class="Menu">
<div id="Menu">
<nav>
<ul class="cssmenu">
<li><a href="Home.php">Home</a></li>
<li><a href="Account.php">Account</a></li>
<li><a href="Projects.php">Projects</a></li>
<li><a href="Users.php">Users</a></li>
<li><a href="LogOut.php">LogOut</a></li>
</ul>
</nav>
</div>
</div>
<div class="LeftBody">
<form id="form1" name="form1" method="post" enctype="multipart/form-data">
<div class="FormElement">
<input name="FirstName" type="text" class="TField" id="FirstName" placeholder="First Name" value="<?php echo $_SESSION["FirstName"]; ?>">
</div>
<div class="FormElement">
<input name="LastName" type="text" class="TField" id="LastName" placeholder="Last Name" value="<?php echo $_SESSION["LastName"]; ?>">
</div>
<div class="FormElement">
<input name="Email" type="email" class="TField" id="Email" placeholder="Email Address" value="<?php echo $_SESSION["Email"]; ?>">
</div>
<div class="FormElement">
<input name="JobRole" type="text" class="TField" id="JobRole" placeholder="Job Role" value="<?php echo $_SESSION["Role"]; ?>">
</div>
<div class="FormElement">
<input name="Password" type="password" class="TField" id="Password" placeholder="Password" required="requried">
</div>
<div class="FormElement">
<input type="file" name="file">
<br>
<br>
</div>
<div class="FormElement">
<input name="Update" type="submit" class="button" id="Update" value="Submit Changes">
</div>
</form>
</div>
<div class="RightBody">
<form id="form2" name="form2" method="post" enctype="multipart/form-data">
<p><h3>Skills:</h3>
<?php
//advice given from stackoverflow. Suggests looping around the results of this to output
$result1 = $con->query("SELECT skills.SkillID, skills.Description, COUNT(userskills.SkillID) AS SkillUserHas, MAX(Experience) AS Experience
FROM
(
SELECT 1 AS SkillID, 'Java' AS Description
UNION
SELECT 7 AS SkillID, 'iOS' AS Description
UNION
SELECT 9 AS SkillID, 'PHP' AS Description
UNION
SELECT 3 AS SkillID, 'SQL' AS Description
UNION
SELECT 4 AS SkillID, 'Windows' AS Description
UNION
SELECT 5 AS SkillID, 'Linux' AS Description
UNION
SELECT 6 AS SkillID, 'Unix' AS Description
UNION
SELECT 8 AS SkillID, 'Requirements Elicitation' AS Description
) skills
LEFT OUTER JOIN userskills
ON skills.SkillID = userskills.SkillID AND userskills.UserID = '$User'
GROUP BY skills.SkillID, skills.Description
ORDER BY FIELD(skills.SkillID, 1, 7, 9, 3, 4, 5, 6, 8)")
or die(mysqli_error($con));
while ($skillrow = $result1->fetch_assoc())
{
?>
<div class="CheckboxText">
<?php
echo '<label>';
echo '<input type="checkbox" name="'.$skillrow['Description'].'" id="CheckboxGroup1_'.$skillrow['SkillID'].'" class="skillselect" value="yes" '.(($skillrow['SkillUserHas'] > 0) ? 'checked' : '').'>';
echo $skillrow['Description'].'</label>';
echo '<input type="number" name="'.$skillrow['Description'].'exp" class="expnumber" placeholder="Enter Experience in years." value="'.$skillrow['Experience'].'">';
echo '<br />';
echo '<br />';
}
?>
</div>
</p>
</form>
</div>
<div class="Footer">
<footer class="footer-basic-centered">
<p class="footer-company-motto">We Always Believe</p>
<p class="footer-links"> <a href="Home.php">Home</a> · <a href="Account.php">Account</a> · <a href="Projects.php">Projects</a> · <a href="Users.php">Users</a> · <a href="LogOut.php">LogOut</a> </p>
<p class="footer-company-name">Project Mainframe © 2016</p>
</footer>
</div>
</div>
</body>
</html>