duaabhuv188411 2016-06-29 11:10
浏览 48
已采纳

从PHP函数输出多个值

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);
}
  • 写回答

2条回答 默认 最新

  • douanye8442 2016-06-29 11:22
    关注

    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').

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单