Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    SQL Syntax issue.

    I'm working on consolidated some code that is repeated in about 10 files into a function, and then simply calling the function in each file. Yet I keep getting a syntax error and I'm confused why that would be.

    This is the original code,which works fine:
    PHP Code:
    //  First get the game Type
    $sql 'SELECT style FROM games WHERE id = '.$_SESSION['game_id'];
    $game_style get_one($sql);

    switch(
    $game_style){
        case 
    'R1':         $country_table 'r1_countries';
                        
    $mission_table 'r1_missions';
                        
    $continents_table 'r1_continents';
                           
    $num_countries '43';
                        break;
        case 
    'R2':         $country_table 'r2_countries';
                        
    $mission_table 'r2_missions';
                        
    $continents_table 'r2_continents';
                           
    $num_countries '48';
                        break;
        case 
    'Euro':    $country_table 'euro_countries';
                        
    $mission_table 'euro_missions';
                        
    $continents_table 'euro_continents';
                         
    $num_countries '44';
                            break;
        case 
    'Roman':     $country_table 'roman_countries';
                        
    $mission_table 'roman_missions';
                        
    $continents_table 'roman_continents';
                          
    $num_countries '55';
                          break;
        }    

    $sql "SELECT adjacencies FROM {$country_table} WHERE id = {$from_country} "
    Now, the new function I created.
    PHP Code:
    function country_table() {

        
    $sql 'SELECT style FROM games WHERE id = '.$_SESSION['game_id'];
        
    $game_style get_one($sql);

        switch(
    $game_style){
        case 
    'R1':         $country_table 'r1_countries';
                        
    $mission_table 'r1_missions';
                        
    $continents_table 'r1_continents';
                           
    $num_countries '43';
                        break;
        case 
    'R2':         $country_table 'r2_countries';
                        
    $mission_table 'r2_missions';
                        
    $continents_table 'r2_continents';
                           
    $num_countries '48';
                        break;
        case 
    'Euro':    $country_table 'euro_countries';
                        
    $mission_table 'euro_missions';
                        
    $continents_table 'euro_continents';
                         
    $num_countries '44';
                            break;
        case 
    'Roman':     $country_table 'roman_countries';
                        
    $mission_table 'roman_missions';
                        
    $continents_table 'roman_continents';
                          
    $num_countries '55';
                          break;
        }    

    and the new code with the function:
    PHP Code:
    // First get the tables for the game style
    country_table();

    $sql "SELECT adjacencies FROM {$country_table} WHERE id = {$from_country} "
    Yet I seem to always get a "Failed to get assoc_array: You have an error in your SQL syntax; " error.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,043
    Thanks
    2
    Thanked 316 Times in 308 Posts
    Variables that are created and set inside of a function, only exist inside that function. To do what you want, you would need to create the variables in your main code and then use the global keyword inside the function definition to access them. Add the following like so -
    PHP Code:
    function country_table() { 
    global 
    $country_table$mission_table$continents_table$num_countries// declare the global variables so they can be accessed in the function 
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Bah dont do that, (but you could). Setup an array in your function and put your info in it. Like so:

    PHP Code:
    function country_table() {
        
    $tables = array();

        
    $sql 'SELECT style FROM games WHERE id = '.$_SESSION['game_id'];
        
    $game_style get_one($sql);

        switch(
    $game_style){
        case 
    'R1':      $tables['country_table'] = 'r1_countries';
                        
    $tables['mission_table'] = 'r1_missions';
                        
    $tables['continents_table'] = 'r1_continents';
                        
    $tables['num_countries'] = '43';
                        break;
        } 
        return 
    $tables;   
    }

    //then your call
    // First get the tables for the game style
    $tables country_table();

    $sql "SELECT adjacencies FROM {$tables['country_table']} WHERE id = {$from_country} "
    I also suggest adding in mysql_error() messages since it seems your still new to php and mysql That would of showed you what was failing and make it easier for you to troublshoot.


  •  

    Posting Permissions

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