PBD817 Posted June 15, 2010 Share Posted June 15, 2010 Hello again everyone, How do I show a count of information from three tables on my main web page? There are three data tables (buyers, suppliers, products.) I want to show the number of each on the page similar to this: # buyers = 13,000 # suppliers = 250 # products = 5,000 The point is to have these numbers go up as the quantity increases. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/ Share on other sites More sharing options...
F1Fan Posted June 15, 2010 Share Posted June 15, 2010 Just do a "SELECT count(*) FROM table" for each of them, and display the results. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072638 Share on other sites More sharing options...
PBD817 Posted June 15, 2010 Author Share Posted June 15, 2010 Ok, this is what I am trying to make it look like. Where do I insert the php code to show the counts? (I want to put them in the table next to the associated category. <table width="500" border="0"> <tr> <td width="62"># Buyers</td> <td width="110"> </td> <td width="93"># Suppliers</td> <td width="55"> </td> <td width="111"># Products</td> <td width="43"> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072646 Share on other sites More sharing options...
F1Fan Posted June 15, 2010 Share Posted June 15, 2010 Are you brand-new to PHP programming? Unless I don't understand what you're asking, this seems like a really simple question. Get the counts from the tables, then echo them out. <table width="500" border="0"> <tr> <td width="62"># Buyers</td> <td width="110"><?php echo $buyersCount; ?></td> <td width="93"># Suppliers</td> <td width="55"><?php echo $suppliersCount; ?></td> <td width="111"># Products</td> <td width="43"><?php echo $productsCount; ?></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072650 Share on other sites More sharing options...
PBD817 Posted June 15, 2010 Author Share Posted June 15, 2010 Yes, very new to php. I inherited a website and am trying to learn the code. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072656 Share on other sites More sharing options...
F1Fan Posted June 15, 2010 Share Posted June 15, 2010 If you are so new that you are unfamiliar with even printing a variable on the screen, then you should really go through a tutorial. Here's a good one to get you started: http://www.w3schools.com/php/php_syntax.asp Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072664 Share on other sites More sharing options...
PBD817 Posted June 15, 2010 Author Share Posted June 15, 2010 Thanks for the help. I copied the code in and uploaded it. But it did not work. What am I missing. Is the table reference $buyersCount correct? The table name is Buyers. Sorry for my inexperience. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072665 Share on other sites More sharing options...
B0b Posted June 15, 2010 Share Posted June 15, 2010 <?php $dbUser = 'database_username'; $dbPass = 'database_password'; $database = 'database_name'; $dbCon = mysql_connect( 'localhost', $dbUser, $dbPass ); mysql_select_db( $database, $dbCon ); $tmpQuery = mysql_query( "SELECT count(*) FROM buyers" ); $tmpQuery = mysql_fetch_row( $tmpQuery ); $buyersCount = $tmpQuery[0]; $tmpQuery = mysql_query( "SELECT count(*) FROM suppliers" ); $tmpQuery = mysql_fetch_row( $tmpQuery ); $suppliersCount = $tmpQuery[0]; $tmpQuery = mysql_query( "SELECT count(*) FROM products" ); $tmpQuery = mysql_fetch_row( $tmpQuery ); $productsCount = $tmpQuery[0]; ?> <table width="500" border="0"> <tr> <td width="62"># Buyers</td> <td width="110"><?php echo $buyersCount; ?></td> <td width="93"># Suppliers</td> <td width="55"><?php echo $suppliersCount; ?></td> <td width="111"># Products</td> <td width="43"><?php echo $productsCount; ?></td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072666 Share on other sites More sharing options...
F1Fan Posted June 15, 2010 Share Posted June 15, 2010 As B0b demonstrated, you need to select the data, extract the row, and retrieve the column from the row and save it to those variable names. My example was assuming that you knew the basics of assigning values to variables. Please learn the basics of PHP before continuing to post. Otherwise, you're basically asking us to write your code for you. We are all glad to help users stuck on specific problems, or help them learn new techniques, but we try not to make a habit of just writing things for people. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072671 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks for the responses. I do not want to be a burden in my code learning. Maybe someone can recommend a site for beginners to get help with code? The issue I am running into is that there is an include.php file that has the database connection so it is not necessary to put that in the index.php page. However, I am not getting the data to show. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1072997 Share on other sites More sharing options...
F1Fan Posted June 16, 2010 Share Posted June 16, 2010 If you are looking to learn, follow the tutorial that I sent in my earlier post. If you need help with something specific, post the code you're referring to, and we can go from there. Be sure to include ALL relevant code, but be sure to exclude your DB host/user/pass. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073006 Share on other sites More sharing options...
hcdarkmage Posted June 16, 2010 Share Posted June 16, 2010 A good place to learn the basics is http://www.w3schools.com/. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073010 Share on other sites More sharing options...
mrMarcus Posted June 16, 2010 Share Posted June 16, 2010 <?php $dbUser = 'database_username'; $dbPass = 'database_password'; $database = 'database_name'; $dbCon = mysql_connect( 'localhost', $dbUser, $dbPass ); mysql_select_db( $database, $dbCon ); $tmpQuery = mysql_query( "SELECT count(*) FROM buyers" ); $tmpQuery = mysql_fetch_row( $tmpQuery ); $buyersCount = $tmpQuery[0]; $tmpQuery = mysql_query( "SELECT count(*) FROM suppliers" ); $tmpQuery = mysql_fetch_row( $tmpQuery ); $suppliersCount = $tmpQuery[0]; $tmpQuery = mysql_query( "SELECT count(*) FROM products" ); $tmpQuery = mysql_fetch_row( $tmpQuery ); $productsCount = $tmpQuery[0]; ?> <table width="500" border="0"> <tr> <td width="62"># Buyers</td> <td width="110"><?php echo $buyersCount; ?></td> <td width="93"># Suppliers</td> <td width="55"><?php echo $suppliersCount; ?></td> <td width="111"># Products</td> <td width="43"><?php echo $productsCount; ?></td> </tr> </table> Instead of multiple queries, look to join tables; one query. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073018 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks again for the other websites to use. As simple as this one is, I am struggling with syntax. Here is what I have so far: In the include.php file: $buyerct = query_mysql("SELECT count(*) FROM buyers");$buyerct = mysql_fetch_row( $buyerct ); $buyersCount = $buyerct[0]; $countryct = query_mysql("SELECT count(`Country`) FROM buyers");$countryct = mysql_fetch_row($countryct); $countryCount = $countryct[0]; $supplierct = query_mysql("SELECT count(*) FROM Suppliers");$supplierct = mysql_fetch_row($supplierct); $supplierCount = $supplierct[0]; $productct = query_mysql("SELECT count(*) FROM Products");$productct = mysql_fetch_row($productct); $productCount = $productct[0]; In the index.php there is this code: <table width="634" border="0"> <tr> <td width="75"># Buyers</td> <td width="100"><?php echo $buyersCount; ?></td> <td width="75"># Countries</td> <td width="100"><?php echo $countryCount; ?></td> <td width="75"># Suppliers</td> <td width="100"><?php echo $suppliersCount; ?></td> <td width="75"># Products</td> <td width="100"><?php echo $productsCount; ?></td> </tr> </table> I should change my user ID on these posts to Bad@php Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073021 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 Oh, One other issue, now trying to count the unique countries in the Buyers table. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073023 Share on other sites More sharing options...
F1Fan Posted June 16, 2010 Share Posted June 16, 2010 Whew, this code is tough to follow. It would be much easier to read if you added some new lines, and named your variables something that makes more sense. You are using incorrect function names, specifically "query_mysql" rather than the correct "mysql_query." As for the country count, I'm not sure how it's done in MySQL, but you can try the below, and if it's wrong, perhaps someone else will correct me. $buyerResult = mysql_query("SELECT count(*) FROM buyers"); $buyersRow = mysql_fetch_row( $buyerResult ); $buyersCount = $buyersRow[0]; $countryResult = mysql_query("SELECT count(DISTINCT(`Country`)) FROM buyers"); $countryRow = mysql_fetch_row($countryResult); $countryCount = $countryRow[0]; $supplierResult = mysql_query("SELECT count(*) FROM Suppliers"); $supplierRow = mysql_fetch_row($supplierResult); $supplierCount = $supplierRow[0]; $productResult = mysql_query("SELECT count(*) FROM Products"); $productRow = mysql_fetch_row($productResult); $productCount = $productRow[0]; Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073027 Share on other sites More sharing options...
ignace Posted June 16, 2010 Share Posted June 16, 2010 SELECT count(id) AS count FROM buyers GROUP BY Country UNION SELECT count(id) AS count FROM suppliers UNION SELECT count(id) AS count FROM products -- OR SELECT count(SELECT id FROM buyers GROUP BY Country) AS buyer_count, count(SELECT id FROM suppliers) AS supplier_count, count(SELECT id FROM products) AS product_count; Requires only 1 query instead of 3 Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073028 Share on other sites More sharing options...
F1Fan Posted June 16, 2010 Share Posted June 16, 2010 ignace, I think that's a great solution, and probably the one I would personally use, but PBD817 is so new to PHP (and possibly programming in general) that I think we should start him off a little slower than that. Splitting the queries up may help him understand the flow of PHP/MySQL syntax a little better. Maybe not. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073031 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks for your understanding F1Fan. You are right about being new. I had a website passed on to me and no php experience. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073037 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 I think I found the solution.... How much would I have to pay someone to do this type of ad hoc coding on the fly? Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073044 Share on other sites More sharing options...
ignace Posted June 16, 2010 Share Posted June 16, 2010 There isn't much to it. Not after I posted the above query, now all that's left is to copy-paste and top off with query processing. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073050 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 Thanks again for your help. I am trying to understand the code and where it would be inserted into a php page (index.php or include.php) as well as the query command. I am out of my league. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073056 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 Ok, I am getting close to a record on how long I can do code incorrectly. The database connection is in one file and the data is displaying in another page. I have tried each of the examples posted and am successfully getting a bunch of errors. Would it be better to upload the to pages and see what needs to be fixed? Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073083 Share on other sites More sharing options...
F1Fan Posted June 16, 2010 Share Posted June 16, 2010 Post ALL of the code, and post what errors you're getting. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073089 Share on other sites More sharing options...
PBD817 Posted June 16, 2010 Author Share Posted June 16, 2010 F1Fan, I can post the code or even the php pages. The error messages are displaying on the uploaded page and are obviously just syntax errors on my part. Seriously, I just need to hire someone to do this. I am neglecting my job trying to figure very basic issues. I dont want to turn into a code-baby trying to get others to do this work for me. I just simply do not have the time to sit and learn this trial by error. Quote Link to comment https://forums.phpfreaks.com/topic/204892-how-do-i-display-database-count-on-main-page/#findComment-1073104 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.