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(
Never use global. Pass $connection into your function as an argument.
global
$connection
Logic and representation should be separated. Read about MVC: here or here.
Global variables are evil, never use it. If someone suggests it - ignore all their answers.