simple sql query

Thread Solved

Join Date: Jun 2008
Posts: 13
Reputation: AliHurworth is an unknown quantity at this point 
Solved Threads: 0
AliHurworth AliHurworth is offline Offline
Newbie Poster

simple sql query

 
0
  #1
Aug 5th, 2008
Hi all,
I'm struggling to write an SQL query.
I'd like to search a database for either a person or an organisation.

I have an HTML form on a homepage, and this returns two values:
  • a search term
  • a search type, either 'person' or 'organisation'
code:
  1. <form action="dir_query.php" method="post" name="qsearch" id="qsearch">
  2. <table>
  3. <tr>
  4. <td>Searching for...</td>
  5. <td><input type="text" name="dir_search" id="dir_search" /></td>
  6. </tr>
  7. <tr>
  8. <td>which is...</td>
  9. <td><select name="dir_type" id="dir_type">
  10. <option value="organisation">an organisation's name</option>
  11. <option value="person" selected>a person's name</option>
  12. </select>
  13. <input type="submit" value="submit" /></td>
  14. </tr>
  15. </table>
  16. </form>
So far, so straightforward.
When processed these are converted into variables, and included in the MySQL query string.

The database, a contact list, has a number of fields, including name (a person's name) and organisation.

I'm currently trying:
  1. $sql=(SELECT $dir_search FROM 'directory' WHERE $dir_type="person" OR $dir_type="organisation")

No dice. In fact, like this, no output at all. I've got a feeling that this will involve a table and an array, but then I start to palpitate.

Can anyone help?
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 16
Reputation: mcd is an unknown quantity at this point 
Solved Threads: 6
mcd mcd is offline Offline
Newbie Poster

Re: simple sql query

 
0
  #2
Aug 5th, 2008
First let's dissect sql string you're trying to put togther:

From what it looks like, you're passing $dir_search as the value being searched for, and $dir_type as the field to search on. Of course, with out seeing your table I'm really just guessing here. But if what I think you're trying to get at (just at looking at your html form) is correct,

  1. SELECT * FROM directory WHERE $dir_type LIKE '%$dir_search%'

You see where the asterisk is? That's where you tell the database which fields you want to return. You can name the fields to return, or use the asterisk to tell the db to return all fields in the table.

$dir_type is the name of the field to search on (person or organization - again, I'm guessing).

$dir_search is the actual value you want to run a compare on. I use LIKE instead of = (which is common on db searches) because it will return a partial string match, and is usually case-insensitve. The percentage signs are wildcards.

And a couple more notes - in a sql query, values are enclosed by single apostrophes. You don't have to enclose the table or field names, but if you want to, use the ` character at the top-left of your keyboard. Mostly this character comes in handy for returning aliases for field names, but that's for the next lesson...

Oh wait, one more thing. Enclose the whole string we've just build in double-quotes in the php, so then our final example becomes:

  1. $sql = "SELECT * FROM `directory` WHERE $dir_type LIKE '%$dir_search%'";
  2. $get_data = mysql_query($sql);

and so on...
Last edited by mcd; Aug 5th, 2008 at 10:34 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,072
Reputation: Shanti Chepuru is on a distinguished road 
Solved Threads: 98
Shanti Chepuru's Avatar
Shanti Chepuru Shanti Chepuru is offline Offline
Veteran Poster

Re: simple sql query

 
0
  #3
Aug 6th, 2008
Originally Posted by AliHurworth View Post

  1. $sql=(SELECT $dir_search FROM 'directory' WHERE $dir_type="person" OR $dir_type="organisation")
the syntax you wrote is very wrong....
Data base cloumn names are not start with $ like $dir_search...or if it is a value post form a form ,then we have write like $_POST['var']...
so modify your query as per:
  1. $sql = "SELECT * FROM `directory` WHERE $dir_type LIKE '%$dir_search%'";
  2. $get_data = mysql_query($sql);
mentioned by mcd at the above post....
Last edited by Shanti Chepuru; Aug 6th, 2008 at 1:00 am.
Be intelligent, But Don't try to cheat.. Be innocent But Don't get cheated..
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 13
Reputation: AliHurworth is an unknown quantity at this point 
Solved Threads: 0
AliHurworth AliHurworth is offline Offline
Newbie Poster

Re: simple sql query

 
0
  #4
Aug 6th, 2008
And that is just what I needed: a better undertsnading, and the answer.
Thanks Shanti and mcd!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC