Jump to content

Recommended Posts

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

<?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...";
}

?>

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]

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.