Amount of rows shown on homepage?!!
Moderator: General Moderators
-
gaogier
- Forum Contributor
- Posts: 391
- Joined: Wed Mar 02, 2005 1:02 pm
- Location: Portsmouth, UK
- Contact:
Amount of rows shown on homepage?!!
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?
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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Amount of rows shown on homepage?!!
You want to count the amount of rows in 10 tables? You could make use of alias to limit the number of queries.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.
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?!!
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
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
?>- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Amount of rows shown on homepage?!!
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?!!
so, i do what?
I really need to learn php...
I really need to learn php...
Re: Amount of rows shown on homepage?!!
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.
- EverLearning
- Forum Contributor
- Posts: 282
- Joined: Sat Feb 23, 2008 3:49 am
- Location: Niš, Serbia
Re: Amount of rows shown on homepage?!!
This will return total of your counts:
you need to call mysql_query() with this astring as argument
go to http://www.php.net/manual/en/index.php and start reading about PHP 
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";
Code: Select all
mysql_query($sql);-
gaogier
- Forum Contributor
- Posts: 391
- Joined: Wed Mar 02, 2005 1:02 pm
- Location: Portsmouth, UK
- Contact:
Re: Amount of rows shown on homepage?!!
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?
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";
?>- EverLearning
- Forum Contributor
- Posts: 282
- Joined: Sat Feb 23, 2008 3:49 am
- Location: Niš, Serbia
Re: Amount of rows shown on homepage?!!
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?!!
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
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
- EverLearning
- Forum Contributor
- Posts: 282
- Joined: Sat Feb 23, 2008 3:49 am
- Location: Niš, Serbia
Re: Amount of rows shown on homepage?!!
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.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Amount of rows shown on homepage?!!
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.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"; ?>