How to put mysql inside a php function?

后端 未结 6 1010
梦谈多话
梦谈多话 2021-01-14 15:37

I have problem about putting mysql into a function showMsg(). The mysql is working fine if it is not wrapped by function showMsg(), but when I wrap it with function showMsg(

6条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 16:16

    $connection variable has no value assigned. The following code should solve your problem (add it at the beginning of the function):

    global $connection;
    

    But you should be aware of the fact, that using globals is not a good idea and you may want to:

    • (preferably) pass $connection variable within the parameter of the function, or
    • move $connection declaration from outside the function just into the function (if it does not cause additional problems), or
    • redeclare $connection variable within the function (again: if it will not cause additional problems),

提交回复
热议问题