Fix php and sql errors. closes #2004