1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP MySql query not working

Discussion in 'PHP' started by gilgil2, Apr 18, 2012.

  1. #1
    Hi, I am trying to query a MySql database based on which search fields are filled in and ignoring those that aren't. I have only got as far as the first part and it isn't working, could anyone tell me why?

    if ((isset($_POST['band'])) && (!isset($_POST['day'])) && (!isset($_POST['month'])) && (!isset($_POST['year'])) && (!isset($_POST['location'])))
    
    {
       
    $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%$band%'");       
    
     while($row = mysql_fetch_array($result))     
    
    {         $page = sprintf( "<a href='http://www.example.com/%s'>%s</a>" , $row['PageName']  , $row['band']  ) ;     
    
       echo 'Band Name:  ';         echo $row['band'] ." " . " " . $row['day'] . " " . $row['month'] . " " . $row['year']. " " . $row['Genre'];   
    
         echo $page;  
     
           echo "<br />";    }
    }
    
    else echo 'No gigs found' ;
    
    PHP:
    Thanks in advance
     
    gilgil2, Apr 18, 2012 IP
  2. Yuuko008

    Yuuko008 Member

    Messages:
    682
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    33
    #2
    Try these:

    1.] Make sure that your if condition is actually retrieving "true", having a long and multiple "&&" and "||" might be confusing.
    2.] Try changing
    $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%$band%'"); 
    PHP:
    to
    $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%{$band}%'"); 
    PHP:
     
    Yuuko008, Apr 18, 2012 IP
  3. xent1986

    xent1986 Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi.
    It seems to me that your code must be changed to:
    $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%".$_POST['band']."%'");
    PHP:
    If my reply helps you add to my reputation, please.
     
    xent1986, Apr 18, 2012 IP
  4. gilgil2

    gilgil2 Member

    Messages:
    71
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Thanks for the responses, with your help and using empty() I have got it sorted, now I have a different problem though.

    If I search for data that is in database of one of the conditions it doesn't find anything, I think the problem is to with this part:

    
    $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%".$_POST['band']."%' AND day='%".$_POST['day']."%' AND month='%".$_POST['month']."%' AND year='%".$_POST['year']."%'");
    
    PHP:
    The day and year are numerical and month is text. I have tried removing ' ' from the numerical ones but that did work, or I have done it wrong. Any ideas?

    Thanks
     
    gilgil2, Apr 19, 2012 IP
  5. Yuuko008

    Yuuko008 Member

    Messages:
    682
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    33
    #5
    Hi,

    First assign your $_POST values to variables so you can get cleaner code. i also don't know why you add % on day, month and year. Aren't you supposed to get the exact values on those fields? Try using these code.

    $band_name = $_POST['band'];
    $gig_day = $_POST['day'];
    $gig_month = $_POST['month'];
    $gig_year = $_POST['year'];
    
    $result = mysql_query("SELECT * FROM gigs WHERE band LIKE '%{$band_name}%' AND day='{$gig_day}' AND month='{$gig_month}' AND year='$gig_year'");
    PHP:
    Also be sure the the three condition, $gig_day AND $gig_month AND $gig_year, is true. Check your database, if such column do exist.
     
    Yuuko008, Apr 19, 2012 IP