【问题标题】:Pass parameter to function using URL使用 URL 将参数传递给函数
【发布时间】:2018-06-10 08:07:46
【问题描述】:

这是我试图从 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'] 为什么?

【问题讨论】:

  • 尝试在 PHPMyAdmin 中直接对数据库进行查询,以确认返回了哪些行(如果有)
  • 它的工作......我从 index.php 调用该函数,他返回了我想要的。但为什么它不能从 url 调用中工作?
  • 为什么你用$childId而不是$_GET['childId']调用函数?

标签: php function url call


【解决方案1】:

我认为您的意思是编写第二个 sn-p 代码 像这样:

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;
    }
}

【讨论】:

  • 它不起作用,即使是 echo 的输出,一切都消失了
  • 那是因为你 return 来自你的函数之前 echo
  • @u_mulder 我删除了回声它仍然没有显示任何东西
  • 它应该显示什么?你在哪里输出函数结果,嗯?
猜你喜欢
  • 1970-01-01
  • 2013-01-27
  • 1970-01-01
  • 2014-05-25
  • 2015-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-18
相关资源
最近更新 更多