Log in

View Full Version : Resolved Query database for total records from all tables



TheJoshMan
03-22-2009, 09:15 PM
Ok, I'm completely lost. I've tried everything I can think of, but I can't figure out how to query my database and return the total number of records (the sum of all tables)

Any help would be greatly appreciated... I'd like to run this query via a php script so I can print the result on my site.

Here's an image that shows you exactly what I'm talking about: http://e7t.us/database-img.png


Thanks!

Master_script_maker
03-22-2009, 09:38 PM
try using mysql_num_rows (http://us3.php.net/mysql_num_rows). e.g.
$query = mysql_query("SELECT * FROM table");
$number=mysql_num_rows($query); and incorporate that with this: http://www.ebrueggeman.com/blog/mysql/drop-all-tables-in-mysql/
added code:


/* fill in your database name */
$database_name = "my_db";
$number=0;
/* connect to MySQL */
if (!$link = mysql_connect("db_host_name", "username", "pass")) {
die("Could not connect: " . mysql_error());
}

/* query all tables */
$sql = "SHOW TABLES FROM $database_name";
if($result = mysql_query($sql)){
/* add table name to array */
while($row = mysql_fetch_row($result)){
$found_tables[]=$row[0];
}
}
else{
die("Error, could not list tables. MySQL Error: " . mysql_error());
}

/* loop through and drop each table */
foreach($found_tables as $table_name){
$sql = "SELECT * FROM $database_name.$table_name";
if($result = mysql_query($sql)){
$number+=mysql_num_rows($result);
}
else{
echo "Error finding rows in $table_name. MySQL Error: " . mysql_error() . "";
}
}

TheJoshMan
03-22-2009, 09:56 PM
Ok, i just tried that and it's not echoing anything... not even an error to let me know there's a problem...

Master_script_maker
03-22-2009, 09:59 PM
well if nothing echos then there were no errors and the number was saved to the variable $number, you would have to echo that

TheJoshMan
03-22-2009, 10:02 PM
dude, you are a god... plain and simple. you've just helped ensure a timely launch of my new site...

woot woot!

Master_script_maker
03-22-2009, 10:29 PM
you're welcome! glad I could help :)

TheJoshMan
03-25-2009, 02:47 AM
Thanks again, just wanted to share the results: http://e7t.us

w00t w00t!