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(
Because variables are local to functions. You need to add this inside your function:
global $connection;