SQL
If you want a count, ask for a count. Don't ask the server to send all the data for all the rows, just to have the client count the rows. (A few hundred rows is a trivial amount of data for a database to handle, but still, you should follow good practices.)
SELECT SUM(*) AS obj_count
FROM (
SELECT COUNT(article_active)
FROM wcx_articles WHERE article_active = '0'
UNION ALL
SELECT COUNT(video_active)
FROM wcx_videos WHERE video_active = '0'
UNION ALL
SELECT COUNT(app_active)
FROM wcx_apps WHERE app_active = '0'
UNION ALL
SELECT COUNT(link_active)
FROM wcx_links WHERE link_active = '0'
) AS wcs_objects;
Schema
If in fact all four of your tables are union-compatible with each other, then you should strongly consider merging them into one table with a wcx_type
attribute that declares what type of object that row represents.
Are your …_active
columns really strings? Those columns should be of the BOOLEAN
(i.e., TINYINT(1)
) type. The query should be adjusted accordingly: use …_active = 0
instead of …_active = '0'
.
PHP
You reused the $query
parameter to store the result of the query. Don't be stingy like that — it's confusing. Use a new variable to store the result.
You are mixing the object-oriented and procedural styles of mysqli. Be consistent.