Dragen Posted June 10, 2007 Share Posted June 10, 2007 Hi, I've got this function which gets an array <?php //get variable arrays from database function get_variable_arrays($varname){ $sql = "SELECT * FROM variables WHERE `name` = '" . $varname . "' ORDER BY `value` ASC"; if($result = mysql_query($sql)){ if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $variable[$varname][$row['value']] = $row['value']; } }else{ echo 'ERROR: array not found!'; } }else{ echo mysql_error(); } } ?> I think I'm doinf something wrong when calling it as I'm trying to then use the array I've created. If I try: <?php get_variable_arrays('adspacea'); print_r($variable); ?> I just get nothing. I've tried putting a return in my function, but think I'm doing it wrong. Could someone help me to get the $variable array I've created to use it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/55007-solved-getting-an-array-from-within-a-function/ Share on other sites More sharing options...
chigley Posted June 10, 2007 Share Posted June 10, 2007 <?php function get_variable_arrays($varname){ $variable = array(); $sql = "SELECT * FROM variables WHERE `name` = '" . $varname . "' ORDER BY `value` ASC"; if($result = mysql_query($sql)){ if(mysql_num_rows($result) > 0){ while($row = mysql_fetch_assoc($result)){ $variable[$varname][$row['value']] = $row['value']; } return $variable; }else{ echo 'ERROR: array not found!'; return false; } }else{ echo mysql_error(); return false; } } $resultarray = get_variable_arrays('adspacea'); if($resultarray) { print_r($resultarray); } else { echo "An error occured..."; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/55007-solved-getting-an-array-from-within-a-function/#findComment-271938 Share on other sites More sharing options...
wildteen88 Posted June 10, 2007 Share Posted June 10, 2007 You have not coded your function to return anything and thus you are getting nothing. Functions have there own scope so any variables you set in the function will not be abled to be accessed globaly unless you set them as global or return the variable. EDIT: I'd change your code to this: <?php //get variable arrays from database function get_variable_arrays($varname) { $sql = "SELECT * FROM variables WHERE `name` = '" . $varname . "' ORDER BY `value` ASC"; $result = mysql_query($sql) or die(mysql_error()); if(mysql_num_rows($result) > 0) { while($row = mysql_fetch_assoc($result)) { $variable[$varname][$row['value']] = $row['value']; } return $variable; } else { return false; } } $arrayVar = get_variable_arrays('adspacea'); if(is_array($arrayVar)) { print_r($arrayVar); } else { echo 'No results returned'; } ?>[code] [/code] Quote Link to comment https://forums.phpfreaks.com/topic/55007-solved-getting-an-array-from-within-a-function/#findComment-271939 Share on other sites More sharing options...
Dragen Posted June 10, 2007 Author Share Posted June 10, 2007 Thanks! I'm just getting used to using functions as most of my code hasn't used them.. (which I'm currently changing) So I was just confused about return; Thanks Quote Link to comment https://forums.phpfreaks.com/topic/55007-solved-getting-an-array-from-within-a-function/#findComment-271950 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.