Mysql Query Problem

Discussion in 'MySQL' started by SpankedNutz, Oct 19, 2005.

  1. #1
    Hi there,

    This is what I want to do. I want to have one template php with the mysql query below:
    The idea is that i can call one template file without physically having the files on the server:

    I have this defined at the top of the page:

    $cat = array('clothes','electronics');


    And this is the query i want to run:
    ps. This part from the query doesn't work Niche='$cat'
    If i change the Niche='$cat' to Niche='clothes' then it works.

    $sql = "SELECT Sitename_open, Sitename_closed, Message, Niche, IF (CHAR_LENGTH(Message)>100,RPAD(LEFT(Message,50),53,'.'),Message) AS Message FROM xxxxxxx where Niche='$cat' ORDER BY `Sitename_open` ASC";

    I hope someone can help me
     
    SpankedNutz, Oct 19, 2005 IP
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #2
    You would need the where clause to be:
    
    where Niche = 'clothes' or Niche = 'electronics'
    
    Code (sql):
    or
    
    where Niche in ('clothes', 'electronics')
    
    Code (sql):
    you could probably do
    
    $sql = "SELECT Sitename_open, Sitename_closed, Message, Niche, 
    IF (CHAR_LENGTH(Message)>100,RPAD(LEFT(Message,50),53,'.'),Message) AS Message 
    FROM xxxxxxx 
    where Niche in ('"  . implode("','",  $cat) . "') 
    ORDER BY `Sitename_open` ASC";
    
    Code (php):
     
    dct, Oct 19, 2005 IP
  3. SpankedNutz

    SpankedNutz Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    That seems to work, but now i get all the categories in one single page.
    I wanted this page to show only the items where Niche = Clothes when i open the virtual page :clothes.php

    Any ideas how to do this?

    Cheers
     
    SpankedNutz, Oct 19, 2005 IP