#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    15
    Rep Power
    0

    Question about variable names in query


    When I run this query (php/mssql) - I get an error for "undefined variable opt_out_m002... There should be 260 opt out variables returned... Can anyone see what I am doing wrong? Thanks!

    PHP Code:
    // QUERY OPT OUT CATEGORIES 

    $tsqloocat "SELECT * FROM [abc].[dbo].[my_db]  WHERE  (id_service=?);";  

    // SET PARAMETERS AND OPTIONS 
    $paramsoocat = array($id_service); 
    $optionsoocat =  array( "Scrollable" =>SQLSRV_CURSOR_KEYSET );  

    // EXECUTE QUERY 
    $stmtoocat sqlsrv_query($conn$tsqloocat$paramsoocat$optionsoocat); 

    if( 
    $stmtoocat === false ) { die( print_rsqlsrv_errors(), true )); }  

    // SET ROW COUNT
    $row_countoocat sqlsrv_num_rows$stmtoocat );  

    // NO ROWS
    if ($row_countoocat === false) {echo "Error in retrieving row count."; }         

    // ROW(S) FOUND - START 
    elseif ($row_countoocat 0) {  
    $count '000';      

         
    // LOOP - SET TERMS - START     
         
    while( $rowoocat sqlsrv_fetch_array$stmtoocatSQLSRV_FETCH_ASSOC)) {          

         
    // SET VARIABLES        ${'opt_out_m'.sprintf("%03d",$count++)}=$rowoocat['opt_out_m'.sprintf("%03d",$count++)];                    echo ${'opt_out_m'.sprintf("%03d",$count++)};      // LOOP - SET TERMS - END     }  // ROW(S) FOUND - SET TERMS - END } 
    Last edited by laural4705; March 24th, 2013 at 10:36 AM. Reason: Duplicate Post
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,690
    Rep Power
    6351
    PHP Code:
    ${'opt_out_m'.sprintf("%03d",$count++)} 
    Don't ever do anything like this. Use arrays, that's what they're for.

    I don't know why you closed your own thread before getting any replies, but that's the answer. Variable variables are difficult to understand and unnecessary.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo