Hey all,

Looking for some quick guidance, Trying to make it so that I can have a field which presents how many customers the database has.

E.g

This is the DB:

http://prntscr.com/o2vuf

This is my php:

                        <?PHP 
                        $id_customers = "SELECT id FROM Customers";
            $Customeramount = mysql_query($id_customers) or die(mysql_error()); 
            echo $Customeramount;
            ?>

Result I am getting:

Resource id #2

Recommended Answers

So is this solved?

Jump to Post

All 10 Replies

Re: Write number of results in a MYSQL DB In PHP 80 80

i think its a bad idea to store no.of results in a seperate column. why because it has to modify seperately each and every customer is added to database as well as remove from the database.internally it increases no.of operation on a table

can you tell me the reason for your requirement?

so that i give the proper response to your question

Re: Write number of results in a MYSQL DB In PHP 80 80

Basically to show how many customers are in the database ^^

Re: Write number of results in a MYSQL DB In PHP 80 80

for that you have a query like count(*)....... something like this

or you can write some user_defined query to get the unique customers also

i think thats not a good way to store no.of customers in database (as of my knowledge)

Re: Write number of results in a MYSQL DB In PHP 80 80
Member Avatar

COUNT(id) would be my take. You can use GROUP BY functions to subtotal uniques.

Re: Write number of results in a MYSQL DB In PHP 80 80

Heres my revised code:

               <?PHP 
               $sql = "SELECT COUNT(ID)FROM Customers";
               $Customeramount = mysql_query($sql) or die(mysql_error());
               echo $Customeramount ;
                ?>

It still returns this:
http://prntscr.com/o326w

Sorry guys

Re: Write number of results in a MYSQL DB In PHP 80 80
Member Avatar

Stop providing screenshots. Place the answer in the editor. Your site will change and this thread will be worse than useless for others.

SELECT COUNT(id) FROM Customers

Should be valid - it works for my tables, even with the missing space and wrong case. Did you mis-spell the table or the field?

Re: Write number of results in a MYSQL DB In PHP 80 80

Still not working, should I post my whole script?

Re: Write number of results in a MYSQL DB In PHP 80 80

FIX:

                        <?PHP 
            $CustomerTableAmount = mysql_query("SELECT COUNT(ID) FROM Customers") or die(mysql_error());
            $row = mysql_fetch_row($CustomerTableAmount);
            if($row)
            echo $row[0];
            ?>

Basically I wasn't using Mysql_Fetch_Row

Re: Write number of results in a MYSQL DB In PHP 80 80
Member Avatar

So is this solved?

Re: Write number of results in a MYSQL DB In PHP 80 80

Did click Solve, obviously didn't load properly :s

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.