简体   繁体   中英

Finding the number of rows of each user in MYSQL

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/>";
} 
  ?>

I think problem is in following line

mysql_select_db("dbprefix_".$bName) or die("MySQL Error: " . mysql_error());

I think this line will be

mysql_select_db("dbprefix_".$dbName) or die("MySQL Error: " . mysql_error());

Seems that there is a typo here:

 mysql_select_db("dbprefix_".$bName) or die("MySQL Error: " . mysql_error()); 

Did you mean "dbprefix_".$dbName instead of $bName?

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).

You don't need to call

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

every time, just call mysql_select_db for each database and PHP will reuse the connection

This is a copy paste from php manual may be it helps you

If you use implode() with the return value by mysql_fetch_array,

if you use MYSQL_BOTH on parameter 2, the result is not really what you're expecting.

For example: my sql database contains "Amine, Sarah, Mohamed";

$array = mysql_fetch_array($resource,MYSQL_BOTH); 
or $array = mysql_fetch_array($resource); 
echo implode(" - ", $array); 

the result is: Amine-Amine-Sarah-Sarah-Mohamed-Mohamed and we expect just: Amine-Sarah-Mohamed

You must use MYSQL_NUM or MYSQL_ASSOC on parameter 2 to resolve the problem.

Seems rather inefficient to start up a new connection, using the same user/password for every user you've got. MySQL is perfectly capable of querying across different databases from the same connection:

mysql_connect(...);
$sql = "SELECT user_name FROM userinfo";
$result = mysql_query($sql) or die(mysql_error()) {

while($row = mysql_fetch_assoc($result)) {
   $username = $row['user_name'];
   $sql2 = "SELECT count(*) AS cnt FROM dbprefix_{$username}.products";
   $result2 = mysql_query($sql2) or (die(mysql_error());
   echo "$username has {$result2['cnt']} products";
}

In short, doing

SELECT somedb.sometable.somefield

is the same as doing

mysql_select_db('somedb');
SELECT sometable.somefield;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM