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 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图