Page 1 of 1

Amount of rows shown on homepage?!!

Posted: Sun Feb 24, 2008 2:48 pm
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?

Re: Amount of rows shown on homepage?!!

Posted: Sun Feb 24, 2008 4:33 pm
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
 

Re: Amount of rows shown on homepage?!!

Posted: Sun Feb 24, 2008 5:08 pm
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
?>

Re: Amount of rows shown on homepage?!!

Posted: Sun Feb 24, 2008 5:15 pm
by John Cartwright
that is a mysql query string, not php code :S

Re: Amount of rows shown on homepage?!!

Posted: Sun Feb 24, 2008 5:29 pm
by gaogier
so, i do what?

I really need to learn php... :P

Re: Amount of rows shown on homepage?!!

Posted: Sun Feb 24, 2008 11:47 pm
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.

Re: Amount of rows shown on homepage?!!

Posted: Mon Feb 25, 2008 7:18 am
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 :)

Re: Amount of rows shown on homepage?!!

Posted: Mon Feb 25, 2008 8:06 am
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";
 
?>

Re: Amount of rows shown on homepage?!!

Posted: Mon Feb 25, 2008 8:13 am
by EverLearning
Did you read my previous post? Everything you need is there.

Re: Amount of rows shown on homepage?!!

Posted: Mon Feb 25, 2008 8:34 am
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

Re: Amount of rows shown on homepage?!!

Posted: Mon Feb 25, 2008 4:59 pm
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.

Re: Amount of rows shown on homepage?!!

Posted: Mon Feb 25, 2008 6:59 pm
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.