Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) on line 305

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) on line 305

我刚刚在我的网页上添加了一个新的 query,它使用 while loop 来生成 array 结果。但是,当我尝试 运行 页面时,出现错误:"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 64 bytes) on line 305"。我无法弄清楚为什么会突然发生这种情况,或者内存泄漏可能是什么。我不想增加 PHP memory_limit.

的大小

我的问题不同...我要就可能导致内存泄漏的原因寻求建议,而不是这个致命错误是什么或意味着什么。

有问题的查询:

<?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;
            }
?>

整页:

<?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 Whosebug. 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 &copy; 2016</p>
                </footer>
        </div>
</div>
</body>
</html>

请注意,根据文档,如果结果集中没有其他行,mysqli_fetch_array returns NULL。您正在专门检查 false 而不是等同于 false 的东西。所以你有一个无限循环。将其更改为以下内容作为短期修复,直到您更正其他问题为止:-

while (($skillrow = mysqli_fetch_array($result1, MYSQLI_NUM)) != false)
{
    $current_skills[] = $skillrow;
}

做剩下的作为答案,虽然它更像是评论,但太长了。

您的代码有大量重复代码。

例如,您为每种不同的技能执行与下面几乎相同的代码:-

        //If the Unix checkbox is checked it will check to see if Unix is already a skill assigned to the user. If so it will ignore, if not it will add.   
    if (isset($_POST['unix'])){
        if (empty($_POST['unixexp'])){
            $unixexp = $default;
            }else{
            $unixexp = $_POST['unixexp'];}

        $sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = 6") 
        or die(mysqli_error($con));

        $row = mysqli_fetch_assoc($sql);

        if ($row ['total'] == "0"){

        $sql = $con->query("INSERT INTO userskills ( UserID, SkillID, Experience) VALUES  ($User, 6, $unixexp)");

    //If the Unix checkbox is not checked it will check to see if Unix is already a skill assigned to the user. If they are it will delete it. If not it will ignore.   
    }} else{

        $sql = $con->query("SELECT count(UserID) as total FROM userskills WHERE UserID = $User AND SkillID = 6") 
        or die(mysqli_error($con));

        $row = mysqli_fetch_assoc($sql);

        if ($row ['total'] == "1"){

        $sql = $con->query("DELETE FROM userskills 
        WHERE UserID = $User AND SkillID = 6");
        }}

这可以通过循环一系列技能轻松完成,或者更好地循环存储这些技能的 table 的查询结果。这将使代码更短、更简单且更易于维护(因为将来您可以将新技能添加到顶部的数组,或者更好地添加到技能的 table 而不必更改脚本本身完全没有)。

此外,如果您拥有 table 技能,您可以按以下方式查询用户技能:-

SELECT a.SkillID, a.SkillName, COUNT(b.SkillID) AS SkillUserHas
FROM all_skills a
LEFT OUTER JOIN auserskills b
ON a.SkillID = b.SkillID
AND b.UserID = '$User'
GROUP BY a.SkillID, a.SkillName 

然后你可以围绕这个结果输出复选框列表,供用户勾选/取消勾选技能(这将 return 每个技能 1 行,无论用户是否有, 如果他们没有该技能,则该列将为 0,如果为他们记录了该技能,则为 >= 1)。

请注意,您似乎还对变量进行了零清理。您需要使用 mysqli_real_escape_string 或等价物,否则结果可能很糟糕,因为用户可能会沉迷于 SQL 注入。

快速尝试清理代码。这只是使用编码的技能列表,而不是将它们存储在 table 中,但希望能给你一些想法。

如果技能 table 将用户 ID / 技能 ID 作为唯一索引可以做得更好,那么您可以只执行 INSERT / on duplicate key update 而不是尝试读取值来决定是否插入或更新一条记录

<?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'];

    $skills_array = array(1=>'Java',
                        7=>'iOS',
                        9=>'PHP',
                        3=>'SQL',
                        4=>'Windows',
                        5=>'Linux',
                        6=>'Unix',
                        8=>'Requirements Elicitation');

    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"></div>
        <div class="RightBody">
            <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>
                <p>
                    <?php

                        $result1 = $con->query("SELECT all_skills.SkillID, all_skills.SkillName, COUNT(userskills.SkillID) AS SkillKnown, MAX(Experience) AS Experience
                                                FROM 
                                                (
                                                    SELECT 1 AS SkillID, 'Java' AS SkillName
                                                    UNION
                                                    SELECT 7 AS SkillID, 'iOS' AS SkillName
                                                    UNION
                                                    SELECT 9 AS SkillID, 'PHP' AS SkillName
                                                    UNION
                                                    SELECT 3 AS SkillID, 'SQL' AS SkillName
                                                    UNION
                                                    SELECT 4 AS SkillID, 'Windows' AS SkillName
                                                    UNION
                                                    SELECT 5 AS SkillID, 'Linux' AS SkillName
                                                    UNION
                                                    SELECT 6 AS SkillID, 'Unix' AS SkillName
                                                    UNION
                                                    SELECT 8 AS SkillID, 'Requirements Elicitation'
                                                ) all_skills
                                                LEFT OUTER JOIN userskills 
                                                ON all_skills.SkillID = userskills.SkillID AND userskills.UserID = '$User' 
                                                GROUP BY all_skills.SkillID, all_skills.SkillName
                                                ORDER BY FIELD(all_skills.SkillID, 1, 7, 9, 3, 4, 5, 6, 8") or die(mysqli_error($con));

                        while ($skillrow = mysqli_fetch_array($result1, MYSQLI_ASSOC))
                        {
                            echo '<label>';
                            echo '<input type="checkbox" name="'.$skillrow['SkillName'].'" id="CheckboxGroup1_'.$skillrow['SkillID'].'" class="skillselect" value="yes" '.(($skillrow['SkillKnown'] > 0) ? 'checked' : '').'>';
                            echo $skillrow['SkillName'].'</label>';
                            echo '<input type="number" name="'.$skillrow['SkillName'].'exp" class="expnumber" placeholder="Enter Experience in years." value="'.$skillrow['Experience'].'">';
                            echo '<br />';
                            echo '<br />';
                        }

                    ?>
                </p>
                <div class="FormElement">
                    <input name="Update" type="submit" class="button" id="Update" value="Submit Changes">
                </div>
            </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 &copy; 2016</p>
            </footer>
        </div>
    </div>
</body>
</html>