It's generally better to use double quotes to avoid having to escape single quotes. Same thing for the $sql .= string below