Jump to content

return number of rows by selecting from multiple tables


php_begins

Recommended Posts

Hello, I need to count the number of rows in my database which has images from different tables. I tried implementing it using the code below.

But mysql_num_rows returns 0. Not sure if my approach is correct

 

$getimagecounttotal="";

$getimagecount="(SELECT image_id FROM R1_REGISTRY_IMAGE )

                        UNION (SELECT image_id FROM R2_REVIEWS_IMAGE )

                UNION (SELECT image_id FROM REGISTRY_IMAGE )

                UNION (SELECT image_id FROM REVIEWS_IMAGE )

                        UNION (SELECT Photo_id FROM Photos )";

        $getimagecount_results= mysql_query($getimagecount) or die(mysql_error());

$getimagecounttotal=mysql_num_rows($getimagecount);

if($getimagecounttotal=="") { $getimagecounttotal="0"; }

echo $getimagecounttotal;

SELECT (
    SELECT COUNT(*) FROM R1_REGISTRY_IMAGE
) + (
    SELECT COUNT(*) FROM R2_REVIEWS_IMAGE
) + (
    SELECT COUNT(*) FROM REGISTRY_IMAGE
) + (
    SELECT COUNT(*) FROM REVIEWS_IMAGE
) + (
    SELECT COUNT(*) FROM Photos
) AS count

Then use any mysql_* function that fetches data from a resultset (not mysql_num_rows).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.