Amount of rows shown on homepage?!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Amount of rows shown on homepage?!!

Post by gaogier »

Hello

I will try and keep this simple as i can.

I have a database, with say 10 tables out of 100, and in these tables, there are, x rows.

I want a script that connects to these tables, and show how many rows there are on my main/home page.

also this code needs to be included....

<?php

require_once ('../mysql_connect.php');//connect to db

?>


Where can i get one, or is it really simple to do?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Amount of rows shown on homepage?!!

Post by John Cartwright »

gaogier wrote:I have a database, with say 10 tables out of 100, and in these tables, there are, x rows.
I want a script that connects to these tables, and show how many rows there are on my main/home page.
You want to count the amount of rows in 10 tables? You could make use of alias to limit the number of queries.

Code: Select all

 
SELECT COUNT(*) AS `numrows` FROM table1
UNION 
SELECT COUNT(*) AS `numrows` FROM table2
UNION 
SELECT COUNT(*) AS `numrows` FROM table3
 
//etc
 
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: Amount of rows shown on homepage?!!

Post by gaogier »

How could it show the total rows? from all the tables (all different table added up the total rows?)

also, i got this error

Parse error: syntax error, unexpected T_STRING in /home/gaogier/public_html/amountofguides.php on line 5

Code: Select all

<?php
 
require_once ('../mysql_connect.php');//connect to db
 
SELECT COUNT(*) AS `numrows` FROM guild
UNION
SELECT COUNT(*) AS `numrows` FROM location
UNION
SELECT COUNT(*) AS `numrows` FROM map
UNION
SELECT COUNT(*) AS `numrows` FROM quest
UNION
SELECT COUNT(*) AS `numrows` FROM skill
UNION
SELECT COUNT(*) AS `numrows` FROM sp
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Amount of rows shown on homepage?!!

Post by John Cartwright »

that is a mysql query string, not php code :S
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: Amount of rows shown on homepage?!!

Post by gaogier »

so, i do what?

I really need to learn php... :P
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Amount of rows shown on homepage?!!

Post by califdon »

Yes, you do. Also MySQL. I suggest that you start by reading a tutorial or six. We are happy to help you when you have a question, but don't expect us to teach you everything from scratch.
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Amount of rows shown on homepage?!!

Post by EverLearning »

This will return total of your counts:

Code: Select all

 
$sql = "SELECT SUM(numrows) FROM 
(
select count(*) as numrows from table1
UNION
select count(*) as numrows from table2
UNION
select count(*) as numrows from table3
) as result";
 
you need to call mysql_query() with this astring as argument

Code: Select all

mysql_query($sql);
go to http://www.php.net/manual/en/index.php and start reading about PHP :)
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: Amount of rows shown on homepage?!!

Post by gaogier »

I can get each row, but i cant seem to get them all to add up? I have done my homework... and found the following code, which works nicely for each table, but how do i get that to add up all the results?

Do i need to use some sort of math code?

Code: Select all

<?php
 
require_once ('../mysql_connect.php');//connect to db
 
$result = mysql_query("SELECT * FROM sp");
 
$num_rows = mysql_num_rows($result);
 
echo "$num_rows Rows\n";
 
?>
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Amount of rows shown on homepage?!!

Post by EverLearning »

Did you read my previous post? Everything you need is there.
gaogier
Forum Contributor
Posts: 391
Joined: Wed Mar 02, 2005 1:02 pm
Location: Portsmouth, UK
Contact:

Re: Amount of rows shown on homepage?!!

Post by gaogier »

Yes, i did, but it did not work, just shown a white page...

I found the code i posted, and it worked just fine but it dose not show all the rows in the database i want it to show
User avatar
EverLearning
Forum Contributor
Posts: 282
Joined: Sat Feb 23, 2008 3:49 am
Location: Niš, Serbia

Re: Amount of rows shown on homepage?!!

Post by EverLearning »

The cause is probably error in your php code of mysql statement. Turn on error reporting in php and post the error you get, and if its not php error, then post the mysql error.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Amount of rows shown on homepage?!!

Post by John Cartwright »

gaogier wrote:I can get each row, but i cant seem to get them all to add up? I have done my homework... and found the following code, which works nicely for each table, but how do i get that to add up all the results?

Do i need to use some sort of math code?

Code: Select all

<?php
 
require_once ('../mysql_connect.php');//connect to db
 
$result = mysql_query("SELECT * FROM sp");
 
$num_rows = mysql_num_rows($result);
 
echo "$num_rows Rows\n";
 
?>
Note you are fetching the entire contents of the database with this query. Notice in the other examples the use of count(*) in the query string.
Post Reply