Use SQL to match PHP output. Fixes #211