0
votes

I Trying to store 3 data values to mysql database using PHP PDO they are just 3 strings name,username,password. and got this fatal error.

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 262144 bytes) in D:\xampp\htdocs\fiverr\order-management-system\classes\contr\UserContr.class.php on line 13

My Current php.ini data limit = ; Maximum amount of memory a script may consume (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;

                }
            }
        }
    }

?>

this is first time got this error. I worked before lots of project with PHP PDO and i never got this error before. i tried to fix changing php.ini different memory_limits but not fixed. its has wasted 2 days for this. please help me to fix this. thank you!

1
Can you show username_exists method code?Lessmore
yes i can. i edited the question please check. thank you!Tharuka Dananjaya
The amount of memory is not the problem; the problem is something is consuming it. Recursion or binary data might be involved. Double check the query works outside of php, try inserting one thing at a time and then add variables until it breaks. I would bet the problem is either the query or the data.Tim Morton
262144 is suspiciously round numberYour Common Sense
and what is the line 13 exactly?Your Common Sense

1 Answers

0
votes

Thanks For Everyone who tried to help me to solve this. Anyway I found the problem. It's my mistake: I have used the same method name inside of UserView.class.php and User.class.php. The method name is username_exists. Here is the problematic code.

//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;

    }

}

Thanks again to all of you!