Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5970059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:19:12+00:00 2026-05-22T20:19:12+00:00

I have databases of users and each has tables. I want to loop through

  • 0

I have databases of users and each has tables. I want to loop through each user and find the number of rows of a particular table common to each. So i connect to the first DB(usersDB) and pick the names of other DB’s from a table(userinfo) row(user_name). I then connect to each DB using the names obtained in userinfo and try to find the number of rows they each have on a particular table(products) common to them. I tried this but shows the same number of rows for all of them. Any help??

<?php
  //db parameters
 $dbhost = "localhost";   
 $dbname = "usersDB";
$dbuser = "root";   
$dbpass = ""; 

mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());  


 //select main db
 $query  = "SELECT user_name FROM userinfo";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_BOTH))
  {
$dbName =$row['user_name'];
  mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
  mysql_select_db("dbprefix_".$bName) or die("MySQL Error: " . mysql_error()); 

// do a query for each db
    $query = mysql_query('SELECT * FROM `products`');        
    $num_rows = mysql_num_rows($query);
   echo $dbName." has".$num_rows."products"."<br/>";
} 
  ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:19:12+00:00Added an answer on May 22, 2026 at 8:19 pm

    this may not be your issue but I noticed you arn’t closing the connection to each database after you query from it. you should assign a variable to mysql_select_db and after you echo close the database like this:

    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());  
    while($row = mysql_fetch_array($result, MYSQL_BOTH)){
       $dbName =$row['user_name'];
       $db = mysql_select_db("dbprefix_".$dbName, $conn) or die("MySQL Error: " . mysql_error()); 
        if( $db ){
            // do a query for each db
            $query = mysql_query('SELECT * FROM `products`');        
            $num_rows = mysql_num_rows($query);
            echo $dbName." has".$num_rows."products"."<br/>";
    
            mysql_close( $db );
        }
    } 
    

    also notice I took the mysql_connect() line out of the while loop because you don’t need to call this more than once. and I added the $conn variable for your mysql_connect() command, this way you can use $conn in your mysql_select_db() statement. This tell the select_db statement which connection to look in for this database (just alittle more secure).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple database with two tables. Users and Configurations. A user has
My idea: I have a database of users, each user has an autoincremented id
I have a rails application where each user has a separate database. (taking Joel
I have a table, users, in an Oracle 9.2.0.6 database. Two of the fields
I have a database with user 'dbo' that has a login name domain\xzy. How
I have a PostgreSQL database whose tables are divided amongst a number of schemas.
i have in the database typical tables like user, usergroup, and they have relations.
I have a big mysql query which needs to trawl through 4 tables to
So imagine you have multiple tables in your database each with it's own structure
I have a script that appends some rows to a table. One of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.