使用URL将参数传递给函数

时间:2018-06-10 08:07:46

标签: php function url call

这是我试图从url

调用它的函数
 public function getParentId($childId)
 {
    $statment = $this->db->prepare("SELECT parent FROM `person` WHERE id = $childId");
    $statment->execute();
    $result = $statment->fetchAll();

    foreach($result as $output){

        return $output['parent'];
    }
    echo 'You call the function from url';

 }

我编写了这段代码来访问url的函数

 if(isset($_GET['action'])){
    include_once('Family.php');
    $object = new Family;

    switch($_GET['action']){
        case 'getId':
        $object->getParentId($childId);
        break;

        default;
        echo 'There is no function with that name';
        break;
    }
}

然后我从Url

传递参数

test.local/Family.php?action=getId&childId=4

输出您可以从url

调用该函数

但他没有返回$ output ['parent']为什么?

1 个答案:

答案 0 :(得分:2)

我认为你打算编写第二段代码 像这样:

if(isset($_GET['action'])){
    include_once('Family.php');
    $object = new Family;

    switch($_GET['action']){
        case 'getId':
        $object->getParentId($_GET['childId']); //<= Edit
        break;

        default;
        echo 'There is no function with that name';
        break;
    }
}