script timout

Discussion in 'PHP' started by Jeremy Benson, Sep 11, 2014.

  1. #1
    Hello,

    I'm having trouble with a script timing out. Not sure what's happening because the line that gives the error is just a PDO object instantiation.

    Would love some help. If there doesn't seem to be anything wrong there I'm not sure what's going on. There is a bit of other code called from custom objects, but I don't think anything too heavy, just some sql calls.

    The function is below.
    $friendHandler->get_friends($user->return_username(), $letter);

    The script calling the function.
      // Set starting letter and page
                          
                           $letter = 'a';
                           $page = 1;
     
                           if(isset($_GET['letter']))
                           {
                          
                              $letter = base64_decode($_GET['letter']);
                          
                           }
                          
                           $friendHandler = new cfriendhandler;
                           $friendHandler->set_letters($user->return_username());                     
                           $letterString = $friendHandler->return_letterstring();
                           echo $letterString;
                          
                           // get and return friends
                          
                           $friendHandler->get_friends($user->return_username(), $letter);
                           $friends = $friendHandler->return_friends();
                          
                           var_dump($friends);
    
    PHP:
    The function.

    function get_friends($userNameSet, $letterSet)
    {
       global $dsn, $dbUserName, $dbPassword;
      
       $dbConnect = new PDO($dsn, $dbUserName, $dbPassword, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
       $sql = $dbConnect->prepare("SELECT `friend` FROM `friends` WHERE `userName` = ? AND `friend` LIKE = ? AND  `status` = ?");
      
       try{
      
         $sql->execute($userNameSet, ''.$letterSet.'%', 'friend');
         $this->friends = $sql->fetchAll();
      
       }catch(\PDOException $e){   }
    
    }
    
    Code (markup):
     
    Jeremy Benson, Sep 11, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    If I don't remember completely wrong, you need to add the '%' to the variable before you put it in the array - hence it should be something like this:
    
    $letterset = '%'.$letterset.'%';
    $sql->execute($userNameSet, $letterset, 'friend') // although that last one could just as well just be put in the query itself
    
    Code (markup):
     
    PoPSiCLe, Sep 11, 2014 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    execute() expects an array with the values, and not the values as arguments.
     
    nico_swd, Sep 12, 2014 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    That is also correct, of course - you'd need to change the argument to:
    
    $sql->execute(array($userNameSet, $letterset, 'friend'));
    
    PHP:
    or, if you're using a newer version of PHP, I think this'll work just fine:
    
    $sql->execute([$userNameSet, $letterset, 'friend']);
    
    PHP:
     
    PoPSiCLe, Sep 12, 2014 IP
  5. Jeremy Benson

    Jeremy Benson Well-Known Member

    Messages:
    364
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    123
    #5
    Cool, stupid bugs. Sorry I didn't get to this earlier. Thanks.
     
    Jeremy Benson, Oct 3, 2014 IP