Page 1 of 1

SQL query question

Posted: Tue Aug 10, 2010 11:48 am
by mrlayance
Ok, I have this code that is part of a form that create dynamic drop downs my my database.

I want to hide the drop down and query 3 options for the form report. I need some help with the sql query.

Code: Select all

 if ((!empty($_GET['deviceuseage']))&&($_GET['deviceuseage'] != 'all')) 
  { 
    $sql .= " and deviceuseage like '". addslashes($_GET['deviceuseage'])."%' "; 
  }
For example deviceuseage can be server, printer and workstation.

I tried without any success.

Code: Select all

 if ((!empty($_GET['deviceuseage']))&&($_GET['deviceuseage'] != 'deviceuseage = 'server' OR deviceuseage = 'printer' OR deviceuseage = 'workstation')) 
  { 
    $sql .= " and deviceuseage like '". addslashes($_GET['deviceuseage'])."%' "; 
  }
Thanks for the help.

Re: SQL query question

Posted: Tue Aug 10, 2010 9:35 pm
by JakeJ
When having query troubles, I always enter my queries directly in to the sql window of phpmyadmin to make sure I'm getting the right data set and that I have a well formed query. If that works, I start troubleshooting my php code.

But what you should put at the end of any query is or

Code: Select all

die(mysql_error());

Re: SQL query question

Posted: Tue Aug 10, 2010 9:54 pm
by SidewinderX
As JakeJ mentioned the or die(mysql_error()) usually helps a lot. I've outlined the typical steps I follow when I'm having issues with queries here: http://www.johnciacia.com/2010/01/12/de ... -in-php-2/

Also as a side note, you should be using mysql_real_escape_string and not addslashes. Chris Shiflett outlined this here: http://shiflett.org/blog/2006/jan/addsl ... ape-string