Help with SQL and how to display in php.

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
rellison
Forum Newbie
Posts: 7
Joined: Sat Dec 11, 2010 5:19 pm

Help with SQL and how to display in php.

Post 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
jankidudel
Forum Commoner
Posts: 91
Joined: Sat Oct 16, 2010 4:30 pm
Location: Lithuania, Vilnius

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

Post 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 ?
rellison
Forum Newbie
Posts: 7
Joined: Sat Dec 11, 2010 5:19 pm

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

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
rellison
Forum Newbie
Posts: 7
Joined: Sat Dec 11, 2010 5:19 pm

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

Post by rellison »

I am using MySQL for the database.
rellison
Forum Newbie
Posts: 7
Joined: Sat Dec 11, 2010 5:19 pm

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

Post 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..
jankidudel
Forum Commoner
Posts: 91
Joined: Sat Oct 16, 2010 4:30 pm
Location: Lithuania, Vilnius

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

Post 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





Post Reply