Page 1 of 1

Help with SQL and how to display in php.

Posted: Sat Dec 11, 2010 5:41 pm
by rellison
Hi..
I have a database that I am working with that has 8 different fields in it. one of those fields is called Mode

The Mode field contains entries like
A
B C
AB1
AA
D2
ect..

I need to create an output so it would list like
Total Mode A entries = xxx
Total Mode AB1 entries = xxxx

I need to come up with the total amount for each type in the mode field.

I am thinking I can do it with this SQL:
SELECT count(*), Mode
FROM ToolLogBook
GROUP BY Mode

But I'm not sure how to display it.

Any help would be appreciated....

TIA Rick

Re: Help with SQL and how to display in php.

Posted: Sat Dec 11, 2010 9:45 pm
by jankidudel
What do you mean not sure how to display ? Yo don't know how to print them on page or you think it will be not correct ?

Re: Help with SQL and how to display in php.

Posted: Sat Dec 11, 2010 10:34 pm
by rellison
I'm not sure how to print them I am assuming that an array will be returned and that is the part I am not understanding. I'm still not sure if the SQL will work but I think from everything i read that this should do what I want it to...

Rick

Re: Help with SQL and how to display in php.

Posted: Sat Dec 11, 2010 11:40 pm
by s.dot
Is it MySQL?
Using PHP, you would use the mysql_query(), mysql_fetch_assoc() or mysql_fetch_array() or perhaps even mysql_result() to deal with the data and echo or print to display to the browser.

If you're not using mysql, there are similar functions available for the database of your choice.

MySQL function list:
http://www.php.net/manual/en/ref.mysql.php

Re: Help with SQL and how to display in php.

Posted: Sun Dec 12, 2010 7:03 am
by rellison
I am using MySQL for the database.

Re: Help with SQL and how to display in php.

Posted: Sun Dec 12, 2010 7:49 am
by rellison
I am also not sure of the correct way to output this so it prints the Mode and the total number or returns for each mode. Sorry for seeming dumb but it's been quite a few years since I have done much PHP programming and this old brain has trouble remembering..

Re: Help with SQL and how to display in php.

Posted: Sun Dec 12, 2010 8:42 am
by jankidudel
<?php

mysql_connect('localhost', 'db', 'pass');
mysql_query('USE db);



$result = (mysql_query('SELECT mode, COUNT(*) FROM toollogbook GROUP BY mode'));
echo "<table border='1'>";
echo "<tr><th>Mode</th><th>Count</th></tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr>";
echo "<td>$row[0]</td><td>$row[1]</td>";
echo "<tr>";
}
echo "</table>";

?>

Code: Select all