dtz46697 2016-06-13 02:40
浏览 233
已采纳

在php函数中返回一个数组

I have following code to retrieve information of user:

function getUserProfile($id) {              
            $sql_query_profile="SELECT * FROM users WHERE id=$id";
            $user_profile=@mysqli_query($dbconfig,$sql_query_profile);
            $user_details_profile=@mysqli_fetch_array($user_profile,MYSQLI_ASSOC);
            return $user_details_profile;
        }

when I call this function, it returns null value.

$user= getUserProfile($_POST['txt_id']);
    var_dump ($user);

can anybody suggest the better idea please?

  • 写回答

2条回答 默认 最新

  • doujuyang1764 2016-06-13 02:48
    关注

    you are trying to access $dbconfig in function which is global variable. To access global variables use global keyword in function or pass config variable to function itself.

    function getUserProfile($id) {  
                global $dbconfig;            
                $sql_query_profile="SELECT * FROM users WHERE id=$id";
                $user_profile=@mysqli_query($dbconfig,$sql_query_profile);
                $user_details_profile=@mysqli_fetch_array($user_profile,MYSQLI_ASSOC);
                return $user_details_profile;
            }
    

    don't use '@' handle as @Epodax suggest, Do not use @ handle your errors instead of suppressing them, debugging is impossible when using @ and we will be unable to help you until you correct this.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部