【问题标题】:Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes)致命错误:允许的内存大小为 536870912 字节已用尽(尝试分配 262144 字节)
【发布时间】:2021-04-03 07:53:22
【问题描述】:

我尝试使用 PHP PDO 将 3 个数据值存储到 mysql 数据库,它们只是 3 个字符串名称、用户名、密码。并得到了这个致命错误。

致命错误:第 13 行的 D:\xampp\htdocs\fiverr\order-management-system\classes\contr\UserContr.class.php 中允许的内存大小为 536870912 字节已用尽(尝试分配 262144 字节)

我当前的 php.ini 数据限制 = ;脚本可能消耗的最大内存量 (128MB) ; http://php.net/memory-limit memory_limit=512M

    // User Modal-------------------------------
    <?php

    require_once "Dbcon.php";

    class User extends Dbcon{

    //check table is empty 

    protected function if_tableEmpty(){

    $sql = "SELECT * FROM user_login";

    $stmt = $this->connect()->query($sql);

    $result = $stmt->rowCount();
    
    if($result > 0){
        return false;
    }else{
        return true;
    }
}

//check username is already exist

protected function username_exists($username){

    if($this->if_tableEmpty() != true){

        $this->username = $username;

    
        $sql = "SELECT username FROM user_login WHERE username = ?";

        $stmt = $this->connect()->prepare($sql);

        $stmt->execute([$this->username]);

        $result = $stmt->rowCount();

        return $result;
    }else{
        return 0;
    }
    
    
        
}

// User Insert
protected function setUser($name, $username, $password, $type, $status){

    $this->name = $name;

    $this->username = $username;

    $this->password = $password;

    $this->type = $type;

    $this->status = $status;

    $sql = "INSERT INTO user_login(name, username, password, type, status) VALUES(?,?,?,?,?)";

    $stmt = $this->connect()->prepare($sql);

    $insert = $stmt->execute([$this->name, $this->username, $this->password, $this->type, $this->status]);

    if($insert == true){

        return true;

    }else{

        return false;
    }

}
}
    // User Controller-------------------------------
    require_once "./classes/modal/User.class.php";

    class UserContr extends User{

    // check username exists

    public function username_exists($username){

    $this->username = $username;

    $result = $this->username_exists($this->username);

    if($result > 0){

        return true;

    }else{

        return false;

    }

}

// create marketer

public function create_marketer($name, $username, $password){

    if($this->username_exists($username) == false){

        $password = password_hash($password, PASSWORD_DEFAULT);

        $type = 'marketer';

        $status = 'active';
        
        $result = $this->setUser($name, $username, $password, $type, $status);

        if($result == true){

            return true;

        }else{

            return false;

        }
    }else{

        return 1;

    }

}
}
    //Posting php file in includes folder

   <?php

    if(isset($_POST['submit'])){

        $name = $_POST['name'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $confirm_password = $_POST['confirm_password'];

        if(!empty($name) && !empty($username) && !empty($password) && !empty($confirm_password)){

            

            if($confirm_password != $password){

                $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                <strong><i class="fas fa-info"></i> Password Not Match!</strong> Confirm Password not matched try again.
                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                </div>';

                echo $alert;

            }else{

                $result = $marketer->create_marketer($name, $username, $password);

                if($result == true){

                    $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <strong><i class="fas fa-check"></i> Success!</strong> Marketer created successfully.
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                    </div>';

                    echo $alert;

                }elseif($result == false){

                    $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <strong><i class="fas fa-info"></i> Failed!</strong> Marketer creating failed try again. If this error persists, please contact the developer.
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                    </div>';

                    echo $alert;

                }elseif($result == 1){

                    $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                    <strong><i class="fas fa-info"></i> Try Again!</strong> Marketer username already exist please try another username.
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                    </button>
                    </div>';

                    echo $alert;

                }
            }
        }
    }

?>

这是第一次遇到这个错误。我在很多 PHP PDO 项目之前工作过,但我以前从未遇到过这个错误。我试图修复更改 php.ini 不同的 memory_limits 但未修复。它为此浪费了 2 天时间。请帮我解决这个问题。谢谢!

【问题讨论】:

  • 你能显示username_exists方法代码吗?
  • 是的,我可以。我编辑了问题,请检查。谢谢!
  • 内存量不是问题;问题是某些东西正在消耗它。可能涉及递归或二进制数据。仔细检查查询在 php 之外是否有效,尝试一次插入一件事,然后添加变量直到它中断。我敢打赌问题出在查询或数据上。
  • 262144 是可疑的整数
  • 第 13 行到底是什么?

标签: php


【解决方案1】:

感谢所有试图帮助我解决此问题的人。反正我发现了问题。这是我的错误:我在 UserView.class.php 和 User.class.php 中使用了相同的方法名称。方法名称是 username_exists。这是有问题的代码。

//this is the method in modal class

protected function username_exists($username){

    if($this->if_tableEmpty() != true){

        $this->username = $username;

    
        $sql = "SELECT username FROM user_login WHERE username = ?";

        $stmt = $this->connect()->prepare($sql);

        $stmt->execute([$this->username]);

        $result = $stmt->rowCount();

        return $result;
    }else{
        return 0;
    }
    
    
        
}

   //This is the method in Controller class

   public function username_exists($username){

    $this->username = $username;

    $result = $this->username_exists($this->username);

    if($result > 0){

        return true;

    }else{

        return false;

    }

}

再次感谢大家!

【讨论】:

  • 所以这是一个递归
猜你喜欢
  • 2021-12-01
  • 2020-04-10
  • 2014-01-06
  • 2018-08-01
  • 1970-01-01
  • 2013-04-17
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
相关资源
最近更新 更多