SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL PDO: Lost connection during query?

    The following code creates the tables as expected, then craps out in the next "try" block while inserting a row into one of those tables. What could be causing this?

    PHP Code:
       try {
        
    $Database = new PDO($dsn,$username,$password,array(PDO::ATTR_PERSISTENT => true));
        
    $Database->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
       }
       catch(
    PDOException $e) {
        die(
    'Installer error connecting to database: ' $e->getMessage());
       }
       try {
        foreach (
    $statements as $statement) {
         
    $statement str_replace("\n","",$statement);
         
    $query $Database->query($statement);
        }
       }
       catch(
    PDOException $e) {
        die(
    'Installer error creating tables: ' $e->getMessage());
       }
       echo 
    "Database tables successfully created!<br />";
       
    $tablePrefix trim(stripslashes($_POST['DB_TBL_PREFIX']),"'");
       
    $adminPwd md5('password');
       
    $statement "INSERT INTO " $tablePrefix "member (username,password,is_admin,is_mod) VALUES ('Administrator','" $adminPwd "','Y','Y')";
       try {
           
    $query $Database->query($statement);
           
    $adminUserId $Database->lastInsertId();
       }
       catch(
    PDOException $e) {
           die(
    'Installer error creating admin user: (' $e->getMessage() . ')<br />SQL = ' $statement);
       }
       echo 
    "Admin user successfully created!<br />"
    Error message:

    Configuration file successfully created!
    Database tables successfully created!
    Installer error creating admin user: (SQLSTATE[HY000]: General error: 2013 Lost connection to MySQL server during query)
    SQL = INSERT INTO ff_member (username,password,is_admin,is_mod) VALUES ('Administrator','5f4dcc3b5aa765d61d8327deb882cf99','Y','Y')
    Thanks in advance!

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Must be yet another bug in the Windows version of PDO. The workaround is to open a new connection after the first "try" block, although the first connection is supposedly a persistent one and it's not likely that it timed out in the few milliseconds it took to create 8 or 9 tables.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not related to your question, but do you realise that PDO features parameterised queries? You should really use this feature.
    Last edited by kyberfabrikken; Jul 1, 2007 at 10:31.

  4. #4
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Not related to your question, but do you realise that PDO features parameterised queries? You should really use this feature.
    Yes. I do use that feature for dynamically-generated and user-supplied data. In this particular case, however, the initial admin password is hard-coded into the script itself. No danger of SQL injection there.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •