Attempt at passing $mysqli to functions failed so I just switched it to using globals instead (not ideal but at least it works now).