LuckySyringe
Technical User
I'm trying to count how many times the same name shows up in a database with different tables for different times (the tables are still formatted the same way).
IE:
John Doe shows up 4 times in the database, Jane Doe shows up 2 times, and Bob Smith shows up 5, so the output would likely be
I've already searched google and tried some functions but to no avail. Below is what I have so far.
Code:
-> LuckySyringe
IE:
Code:
Array
(
[0] => John Doe
[1] => Jane Doe
[2] => John Doe
[3] => Bob Smith
[4] => Bob Smith
[5] => Jane Doe
[6] => Bob Smith
[7] => John Doe
[8] => Bob Smith
[9] => John Doe
[10] => Bob Smith
)
Code:
$total = array("John Doe" => "4", "Jane Doe" => "2", "Bob Smith" => "5")
I've already searched google and tried some functions but to no avail. Below is what I have so far.
Code:
Code:
<?php
//MySQL Connection Settings
mysql_connect ($mysql_host, $mysql_user, $mysql_pass) or die (mysql_error());
mysql_select_db ($mysql_db) or die (mysql_error());
$query = "SHOW TABLES FROM ".$mysql_db;
$result = mysql_query($query) or die (mysql_error());
while ($row = mysql_fetch_row($result)) {
$tableName = $row[0];
print "Table Name: ".$tableName."\n";
$query_2 = "SELECT * FROM ".$tableName." ORDER BY `name`";
$result_2 = mysql_query($query_2) or die (mysql_error());
$query_3 = "SELECT name FROM ".$tableName;
$result_3 = mysql_query($query_3);
$result_4 = mysql_num_rows($result_3);
print "-- Count from ".$tableName.": ".$result_4."\n";
$total_count = $total_count + $result_4;
while ($row_2 = mysql_fetch_assoc($result_2)){
$val['tableName'] = $tableName;
$array[uniqid(rand(), true)] = $row_2;
}
}
print "Total Count: ".$total_count."\n\n";
$i = count($array) > $total_count ? $total_count : count($array);
$cnt = 0;
foreach ($array as $key=>$val){
if ($cnt < $i ) {
$name[$key] = stripslashes($val['name']);
$cnt++;}
else{
exit;}
}
sort($name);
print_r(array_values($name));
@mysql_free_result($result) or die (mysql_error());
@mysql_free_result($result_2) or die (mysql_error());
@mysql_free_result($result_3) or die (mysql_error());
@mysql_free_result($result_4) or die (mysql_error());
@mysql_close() or die (mysql_error());
?>
-> LuckySyringe