...

View Full Version : SQL Syntax issue.



ptmuldoon
03-31-2007, 03:51 PM
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:

// 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.

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:


// 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.

CFMaBiSmAd
03-31-2007, 04:05 PM
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 -

function country_table() {
global $country_table, $mission_table, $continents_table, $num_countries; // declare the global variables so they can be accessed in the function

iLLin
03-31-2007, 04:29 PM
Bah dont do that, (but you could). Setup an array in your function and put your info in it. Like so:



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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum