SQL query question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrlayance
Forum Commoner
Posts: 31
Joined: Mon Dec 07, 2009 11:53 am

SQL query question

Post 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.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: SQL query question

Post 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());
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: SQL query question

Post 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
Post Reply