0

I've been stumped on this error for days now and I can't find a way to fix it. I've tried to implement

  • Singleton database object
  • Closing my connections after queries
  • Trying a persistence connection
  • Setting max connection to 250 (local)
  • Setting max user connections 250 (on local)

Has this occurred to anyone else?


What's weird is the error message on my local server is Connection failed: SQLSTATE[08004] [1040] but the rows are still inserts and database queries work.

On the production server, the error message becomes Connection failed: SQLSTATE[42000] [1203] User db_admin already has more than 'max_user_connections' active connections I know maximum connections is set to 15, but I have consulted support and they have said I have had no connections at the time of this error.

After all my options, can someone check my code to make sure that there's no errors in it that can be creating these errors?

class MyPDO extends PDO{

    const DB_HOST='localhost';
    const DB_PORT='3306';
    const DB_NAME='db_name';
    const DB_USER='db_admin';
    const DB_PASS='SECRET';

    public function __construct(){

        parent::__construct('mysql:host='.MyPDO::DB_HOST.';port='.MyPDO::DB_PORT.';dbname='.MyPDO::DB_NAME,
                            MyPDO::DB_USER,MyPDO::DB_PASS);
        try{
                $this->db = new MyPDO();
                $this->db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
            }catch(PDOException $e){
                //always catching the connection errors
                echo 'Connection failed: ' . $e->getMessage();
            }
    }

    public function query($query){ 
        $args = func_get_args();
        array_shift($args); 
        //check inputs
        var_dump($args);
        $reponse = parent::prepare($query);
        $reponse->execute($args);
        return $reponse;    
    }  
}

Here is the insert query

function InsertUserToSql()
    {
        $ret = $this->db->query("INSERT INTO CD_USERS (email,username,password,fname,lname,dob,gender) VALUES 
        (?,?,?,?,?,?,?)",$this->Email,$this->Username,$this->Password,$this->Fname,
        $this->Lname,$this->Birthday,$this->Gender);
        if($ret){
            echo "insert success";
            return $this->db->lastInsertId();
        } 
        $this->db = null;
    }  
4

2 回答 2

0

You have recursive code: an instance creates a new instance in $this->db, which creates a new instance in its $this->db, which in turn creates another instance, etc. until the deepest one throws an error (or you go beyond the maximum nesting depth in PHP (not in vannilla, but some modules (Zend,xdebug) set a limit), whichever comes first), and you're left with the max N instances which succeeded, of which you probably use only the 2nd one judging by your code (if MyPDO extends PDO, you can just call $this->query(), there's no need for another instance in ->db, and using $this->db->query().

于 2013-10-27T21:23:12.570 回答
0

Don't extend PDO, in fact, never extend an object you don't own. Extending PDO is annoying for people who'll end up using your code, it's completely pointless (what are you trying to do? improving on the clear, and well known PDO API? Or create an object that fits your current needs better - which means: creating an object you won't be able to use again, which defeats the point of OOP)? I've been over quite a few reasons why you shouldn't extend from PDO here, it might be worth a look.

The, your actual problem:

don't create a new instance of a class in its own constructor!

creating a new MyPDO in the MyPDO constructor will call the MyPDO constructor again, which will create a new instance of MyPDO, which will call the MyPDO constructor again, which will create a new instance of MyPDO, which will call the MyPDO constructor again, which will...
This is infinite recursion at work.

Think of it like this: If someone were to tell you this:

"If you move your right foot in front of your left, you have to move your right foot in front of your left foot again"
And then, that sadistic nutcase with a walking fixation would instruct you (at gunpoint):
"Now place your right foot in front of your left"
How long would it take you until you're doing your Grand ecart (or splits), and can't go any further. That's what is happening in your code.

And, slightly off topic, if you encounter a problem, and try to solve it with a singleton in PHP, you're introducing a new problem...

于 2013-10-27T22:01:19.340 回答