how to select data

Discussion in 'PHP' started by snowLee, Dec 14, 2009.

  1. #1
    I want to use php to print out something from database, but I don't know how to do the select. Please give me a hand. This is my table:
    
    CREATE TABLE IF NOT EXISTS `category` (
      `category_id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(100) NOT NULL,
      `parent` int(11) NOT NULL,
      PRIMARY KEY (`category_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;
    
    PHP:
    INSERT INTO `category` (`category_id`, `name`, `parent`) VALUES
    (1, 'Category', 0),
    (2, 'Subcategory', 1),
    (3, 'Tv ', 2),
    (4, 'Plasma Tv', 3),
    (5, 'Old Tv', 3),
    (6, 'Radio', 2),
    (7, 'New Radio', 6),
    (8, 'Old Radio', 6),
    (9, 'Blue Radio', 6),
    (10, 'Green Radio', 6);
    
    PHP:
    I want to select to print out
    1. only the Category: Tv and Radio
    2. only the Subcategory:plasma Tv,Old Tv,New Radio, Old Radio,Blue Radio,Green Radio

    Someone please tell me how will look those 2 mysql select.
    Thank you.
     
    snowLee, Dec 14, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    select name from category where parent = 2
    select name from category where parent = 6
     
    javaongsan, Dec 14, 2009 IP
  3. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    ok, but I want id 3 to be printed too, where is 6
     
    snowLee, Dec 14, 2009 IP
  4. b4x

    b4x Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi, try this:

    
    <?php
        // Select records where parent is = 2 and 6  and select category_id = 3
        $query = mysql_query('SELECT * FROM `category` WHERE `parent` = "2" OR `parent` = "6" OR `category_id` = "3"');
        
        while ($row = mysql_fetch_assoc($query))
        {
            var_dump ($row);
        }
    ?>
    
    PHP:
     
    b4x, Dec 14, 2009 IP
  5. yoes_san

    yoes_san Peon

    Messages:
    443
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    for category, use this statement:
    "select name from category where parent = 2"

    for subcategory use:
    "select name from category where parent = 6 or parent =3"
     
    yoes_san, Dec 14, 2009 IP