#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0

    Need help with creating a counter


    Could someone give me some help with programming a counter.

    We have a machines at a factory. That machines gives out clothes to people that work at the factory. That machine has a database (PostgreSQL) with all the clothes in it, with size etc.

    People go to the machine, press the button of the clothes they want, for example: Sweater Size M.
    The machine gets the Sweater out of the conveyer and gives it to the user.

    We want a screen at the machine that shows the user how many clothes are inside it. for example, Sweater size M is 500 x inside the machine.

    This is an example of some clothes in the database (table: tbl_garment):

    Code:
    garment_id	wear_no	wear_no_bkp	customer_no	artikel_no	size
    00D00829E3E1	3501026	3501026	6010026	95006	54
    00D0083C1DCF	122	99999	6010923	95000	M
    00D0083AC137	28	99999	6010923	95004	M
    00D007F0427E	3701026	3701026	6010026	95006	50
    00D007F45254	122	99999	6010923	95006	48
    00D0083B09A5	116	99999	6010923	95004	XL
    00D0082BE8FF	5804026	5804026	6010026	95004	L
    00D0082B1E8C	3103026	3103026	6010026	95000	L
    00D0082A8553	5804026	5804026	6010026	95006	48
    00D007EF65CA	99999	99999	6010923	95006	48
    00D0082BE9BC	99999	99999	6010923	95000	S
    00D0082B21A2	351	99999	6010923	95000	M
    00D0083B5D4D	99999	99999	6010923	95000	XL
    00D0083B3E1B	56	99999	6010923	95004	S
    00D0083CB082	99999	99999	6010923	95000	M
    00D0085A4A22	354	99999	6010923	95006	54
    Can someone make a simple SQL Query that shows how many clothes are inside the machine?

    So artikel_no 95004 = Sweater (for example)
    and size M = the size.

    We need to know how many Sweaters with size M is in the machine

    If someone can make that query for just 1 piece of clothes, then we can make the rest of it.

    If i am giving to less information, you can ask me for some more information.

    Thanks,

    Jessy

    Code:
    artikel_no	size
    95006	54
    95000	M
    95004	M
    95006	50
    95006	48
    95004	XL
    95004	L
    95000	L
    95006	48
    95006	48
    95000	S
    95000	M
    95000	XL
    95004	S
    95000	M
    95006	54
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Dec 2004
    Posts
    3,031
    Rep Power
    377
    this is a simple SQL query

    SELECT what you want
    FROM table
    WHERE artikel_no =
    AND ...

    I have done some of it, you can figure out the rest
  4. #3
  5. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Code:
    SELECT COUNT(0) 
    FROM tbl_garment
    WHERE artikel_no = 95004
    AND size = 'M'
    Also, this has nothing to do with PHP so I've moved it to PostgreSQL
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    What is that COUNT(0) stuff supposed to do?
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    COUNT(*), last I checked, was slightly slower than COUNT(0). Some people use SUM(1). I haven't actually written a database query in years though.
    (This is apparently an urban legend, see below)
    Last edited by ManiacDan; February 27th, 2014 at 09:29 AM.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Oct 2003
    Location
    Germany
    Posts
    2,788
    Rep Power
    349
    Originally Posted by ManiacDan
    COUNT(*), last I checked, was slightly slower than COUNT(0)..
    That is an urban myth which has never been true.
    I will not read nor answer questions where the SQL code is messy and not formatted properly using [code] tags.
    http://forums.devshed.com/misc.php?do=bbcode#code

    Tips on how to ask better questions:
    http://tkyte.blogspot.de/2005/06/how-to-ask-questions.html
    http://wiki.postgresql.org/wiki/SlowQueryQuestions
    http://catb.org/esr/faqs/smart-questions.html
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0
    Originally Posted by ManiacDan
    Code:
    SELECT COUNT(0) 
    FROM tbl_garment
    WHERE artikel_no = 95004
    AND size = 'M'
    Also, this has nothing to do with PHP so I've moved it to PostgreSQL
    The thing is, that it needs to be showed on a webpage (the counted clothes).
    SQL can only be combined with PHP am i right?

    And then, the output of this code, needs to be in a string??
    Then you can call that string in PHP, am i right on this one too?

    EDIT:
    Something like this?
    Code:
    <?php
    	$counter1 = pg_query($db_connection, "SELECT COUNT(0) FROM tbl_garment WHERE artikel_no = 95004 AND size = M");
    ?>
    Thanks for the piece of code.

    Jessy
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    3
    Rep Power
    0
    Thanks for the help guys

    This worked:

    Code:
    <?php
    	$query = pg_query($db_connection, "SELECT COUNT(0) FROM tbl_garment WHERE artikel_no = 95000 AND size = 'M'");
    	$results = $query or die ("Werkt niet...");
    	$result_array = pg_fetch_row($results);
    	$result_integer = $result_array[ 0];
    	echo "Trui maat 'M' aantal: $result_integer";
    ?>
  16. #9
  17. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    And now read the other replies, please. Thanks.

    It's COUNT(*), not COUNT(0). Also note that this pg_query() stuff is totally outdated and has been replaced with a modern database interface more than a decade ago. If you're working with existing legacy code, I guess you have to stick with the old functions. But if you write the code more or less from scratch, consider leaving the 90s and entering the 21. century.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  18. #10
  19. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    Originally Posted by shammat
    That is an urban myth which has never been true.
    Huh, I stand corrected. Popular urban myth, that.

    Originally Posted by Jacques
    It's COUNT(*), not COUNT(0)
    These expressions are identical, that's what shammat was saying. It doesn't matter which one you use.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  20. #11
  21. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Originally Posted by ManiacDan
    It doesn't matter which one you use.
    ... which is why it makes absolutely no sense to keep using the weird COUNT(0) variant. All that does is cause confusion.

    The expression for counting the number of records is COUNT(*). The expression COUNT(<exp>) has a totally different meaning and is used to count the specific rows for which <exp> is not NULL. That's the the “official” use which is understood by everybody who knows SQL.

    I see no reason for abusing the second syntax for a micro-optimization which obviously doesn't even work.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".

IMN logo majestic logo threadwatch logo seochat tools logo