www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] correct way to bind many columns on PDO

  1. #1
    Join Date
    May 2006
    Posts
    140

    resolved [RESOLVED] correct way to bind many columns on PDO

    I'm trying to learn how to work with PDO queries and I'm selecting a series of columns from a table with the intention of looping through them. I'm binding each column to a variable like this:

    PHP Code:
        $stmt $db -> prepare("SELECT value1, value2, value3, value4, value5, value6, value7, value8, value9, value10 from table"); 
        
    $stmt -> execute();
        
    $stmt -> bindColumn(1$value1);
        
    $stmt -> bindColumn(2$value2);
        
    $stmt -> bindColumn(3$value3);
        
    $stmt -> bindColumn(4$value4);
        
    $stmt -> bindColumn(5$value5);
        
    $stmt -> bindColumn(6$value6);
        
    $stmt -> bindColumn(7$value7);
        
    $stmt -> bindColumn(8$value8);
        
    $stmt -> bindColumn(9$value9);
        
    $stmt -> bindColumn(10$value10);
        
    $stmt -> bindColumn(11$value11);
        
    $stmt -> bindColumn(12$value12); 
    This hardly seems like the proper way to do this however. I could make a loop that binds every value to a variable with the name of the column, but I was wondering if there's something closer to this method I used with MySQLi:

    PHP Code:
    $stmt -> bind_result($value1$value2$value3$value4$value5$value6$value7$value8$value9$value10); 
    Last edited by Iceline; 01-22-2014 at 05:00 PM.

  2. #2
    Join Date
    May 2006
    Posts
    140
    Hm, seems I can use:

    $stmt -> fetchColumn(1)

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    546
    If you do this you can achieve the same:

    while (list($value1,$value2,$value2....) = $stmt->fetch(PDO::FETCH_NUM);

  4. #4
    Join Date
    May 2006
    Posts
    140
    Quote Originally Posted by ginerjm View Post
    If you do this you can achieve the same:

    while (list($value1,$value2,$value2....) = $stmt->fetch(PDO::FETCH_NUM);
    Thanks, I'll try this

  5. #5
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    546
    I left a ; at the end of my while. Obviously it is wrong since you have to have a set of statements as part of the while to handle your row of data.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles