add a check on $val to use mysqli if it exists