Angel Politis
Angel Politis

Reputation: 11313

Output multiple values from PHP function

I have created the following function to fetch data from my database, but its capabilities are limited. Currently it can fetch one value at a time, which is fine for fetching the value of one column of one row, but as I progress with my work, I now want to be able to fetch multiple values in one call.

The Function:

function retrieve($value, $identifier = null) {
    // Check if identifier is given
    $identifier = (is_null($identifier)) ? "`ID` = '{$_SESSION["ID"]}'" : $identifier;

    // Connect to the database
    $connection = connect("limited");

    // Pass query, get result and fetch value out of it
    $query = "SELECT * FROM `users` WHERE $identifier";
    $result = mysqli_query($connection, $query);
    if (mysqli_num_rows($result) > 0) {
        $data = mysqli_fetch_assoc($result);
        return $data[$value];
    }
    mysqli_close($connection);
}

How I currently use it to fetch multiple values:

// Define variables
$x1 = retrieve("x1");
$x2 = retrieve("x2");
$x3 = retrieve("x3");
$x4 = retrieve("x4");
$x5 = retrieve("x5");
$x6 = retrieve("x6");
$x7 = retrieve("x7");
$x7 = retrieve("x8");

I have read other questions here on Stack Overflow, but none of them solves my problem as I use an optional parameter, which makes my life hard. For example, I thought of implementing the splat operator to allow unlimited parameters, but as I use the optional parameter $identifier, I can't make it into something like:

function retrieve($identifier = null, ...$value) {}

because it will use the first parameter as the identifier when I omit it.

I'm sure that regarding performance it would be better if I could fetch all the necessary values in one call of the function retrieve() instead of using it as shown above and that's why I would like to know:

How can I edit this function in order to fetch more values at once?

Calling it like so:

$x = retrieve($y);
$x1 = $y["x1"];
$x2 = $y["x2"];
...


EDIT:

Thanks to Manish Jesani for his help! I used his answer and modified to do exactly what I want. For anyone that may be interested in the future, here's the code:

function retrieve($value, $identifier = null) {
    // Check if identifier is given
    $values = array();
    $identifier = (is_null($identifier)) ? "`ID` = '1'" : $identifier;

    // Connect to the database
    $connection = connect("limited");

    // Pass query, get result and fetch value out of it
    $query = "SELECT * FROM `users` WHERE $identifier";
    $result = mysqli_query($connection, $query);
    if (mysqli_num_rows($result) > 0) {
        $data = mysqli_fetch_assoc($result);
        if (is_array($value)) {    
            foreach($value as $_value) {
                $values[$_value] = $data[$_value];
            }
            return $values;
        }
        else {
            return $data[$value];
        }    

    }
    mysqli_close($connection);
}

Upvotes: 0

Views: 88

Answers (2)

Manish Jesani
Manish Jesani

Reputation: 1347

Please try this:

function retrieve($value, $identifier = null) {
    // Check if identifier is given
    $return = array();
    $identifier = (is_null($identifier)) ? "`ID` = '{$_SESSION["ID"]}'" : $identifier;

    // Connect to the database
    $connection = connect("limited");

    // Pass query, get result and fetch value out of it
    $query = "SELECT * FROM `users` WHERE $identifier";
    $result = mysqli_query($connection, $query);
    if (mysqli_num_rows($result) > 0) {
        $data = mysqli_fetch_assoc($result);
        if(is_array($value))
        {    
            foreach($value as $_value)
            {
                $return[$_value] = $data[$_value];
            }  
        }
        else
        {
            $return[$value] = $data[$value];
        }    
        return $return;
    }
    mysqli_close($connection);
}


$x = retrieve(array("x1","x2","x3","x4","x5","x6"));

Upvotes: 0

Plamen Vasilev
Plamen Vasilev

Reputation: 362

You can call the function with as many parameters you want. Τo do this you have to use func_num_args() to get all of them, as shown below:

function retrieve() {
    $args = func_num_args();
    
    $query = "SELECT '".implode("','", func_get_args())."' FROM `users` WHERE $identifier";
    $result = mysqli_query($connection, $query);
    if (mysqli_num_rows($result) > 0) {
        $data = mysqli_fetch_assoc($result);
        return $data;
    }
    mysqli_close($connection);
}

You can call this function like this: $params = retrieve('x1','x2','x3').

Alternatively, you can retrieve them as variables list($x1, $x2, $x3) = retrieve('x1','x2','x3').

Upvotes: 1

Related Questions