SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2011
    Location
    Portland
    Posts
    148
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Binding array params to query string

    so i have been having some trouble binding my array parameters to my query string i have try'ed using the default pdo bindParams but i could not seem to get it to work right from within a foreach loop. so i currently tried using str_replace and preg_replace but in both cases the params seem to replace then revert back on the second iteration.

    im really lost on what else to try or maybe a different way to go about this task.

    PHP Code:
    public function execute(){
            
    $statement $this->_statements[$this->_query_count][0];
            
    $params    $this->_statements[$this->_query_count][1];
            if(
    count($params) > 0){
                
    // currently where im trying to replace the found placeholders in the query string
                            // with the an assoc array that identifies the the placeholder with the value.
                
    foreach($params as $k => $v){
                    
    $k str_replace(':' '' $k);
                    
    $qs preg_replace('/\:['.$k.']+/i' $v $statement);
                    
                }
                
                
    var_dump($qs);
                
    //var_dump($transaction->execute());
                //$this->_columns_count = $transaction->columnCount();
                //$this->_affected_rows = $transaction->rowCount();    
            
    }else{
                
    $transaction $this->_lid->prepare($statement);
                
    $transaction->execute();
                
    $this->_columns_count $transaction->columnCount();
                
    $this->_affected_rows $transaction->rowCount();    
            }
            
            
    //$this->_statements[$this->_query_count] = $transaction;
            //return $this;
        


  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    A big mistake I made with PDO initially was assuming that I could use placeholders for the field names, it only works for values:

    eg this won't work:

    "update users set ? = ?" you have to do "update users set name = ?"

    Not sure if you are making similar error or not... just saying.


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
  •