Hi, I don't know too much PHP, but the select might have a problem. I think that what you need is using a nested or imbricated select. Something like:
SELECT (SELECT count(*) FROM $books) AS...