Jump to content

How do I display database count on main page?


PBD817

Recommended Posts

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.

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

<?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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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. 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.
Link to comment
Share on other sites

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

 

 

 

 

 

 

Link to comment
Share on other sites

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];

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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? 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.