函数无法返回值 [英] Function fails to return value

查看:117
本文介绍了函数无法返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在函数内外都尝试了一个代码,它显示了一些非常需要的特性.当不在函数内时,它工作得很好,但是当包含在函数中时,什么也不返回.帮我很好地重构功能.

i have tried a code both in and out of the function and it has showed some very quire characteristics.It works perfectly well when out of the function but when included in the function, nothing is returned. Help me reconstruct the function well.

   <?php

    function detgrade($con,$mrk){

     $pk=$con->prepare("SELECT grade FROM grading WHERE ? BETWEEN min_mark AND max_mark");
     $pk->bindValue(1,$mrk);
     $pk->execute();
     $ind=$pk->fetch(PDO::FETCH_ASSOC);
     $gr = $ind['grade'];

    return $gr;
    }
   //calling the function
     echo detgrade($db,87);

   ?> 

推荐答案

获取结果返回一个对象数组,因此在对象数组上获取'grade'键不会返回任何内容.试试

The fetch result returns an array of objects, so grabbing the 'grade' key on an array of objects won't return anything. Try

$gr = $ind[0]['grade'];

这篇关于函数无法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆