Generate sql string to query MySQL DB ?

Thread Solved

Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Generate sql string to query MySQL DB ?

 
0
  #1
Jul 12th, 2007
Hi folks.

I Have a question :

I'm interested how you generate query string to search DB with more than 1 form with LIKE.

For example :

We have $_POST['name'], $_POST['title'], $_POST['phone'], $_POST['email']. How do you generate sql query to search with two criteria ( name and phone ) ?

Thanks.

- Mitko Kostov.
Last edited by MitkOK; Jul 12th, 2007 at 7:47 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #2
Jul 12th, 2007
Do you just mean the sql statement?

Something like this...

$sql="select * from table where name = ' " . $_POST[name] . " ' ";
$sql = $sql . " and phone = ' " . $_POST[phone] . " ';";
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #3
Jul 12th, 2007
Yes, that's right. But what if $_POST['name'] is not set ? And what if there are more fields to check ?

  1. if (isset($_POST['name'])) { $query.="WHERE name LIKE '%$_POST'name']'"; }
  2. else { $query.="AND name LIKE '%$_POST['name']'"; }
  3.  
  4. if (isset($_POST['title'])) { $query.="WHERE title LIKE '%$_POST['title']'"; }
  5. else { $query.="AND title LIKE '%$_POST['title']'"; }
  6.  
  7. if (isset($_POST['email'])) { $query.="WHERE email LIKE '%$_POST['email']'"; }
  8. else { $query.="AND email LIKE '%$_POST['email']'"; }

Now think that we have 15 fields to check. Is there an easy way to check ( loop maybe ) ?
Last edited by MitkOK; Jul 12th, 2007 at 8:40 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #4
Jul 12th, 2007
The previous post is wrong, I'm sorry.

I cannot edit it.
Last edited by MitkOK; Jul 12th, 2007 at 9:41 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #5
Jul 12th, 2007
this is really rough but i think you mean something along these lines

  1. $sql = "select * from table name where ";
  2. $start;
  3.  
  4. if(isset $_POST['name'])
  5. {
  6. $sql = $sql . $_POST['name'];
  7. $start = 1;
  8. }
  9.  
  10. if(isset $_POST['phone'])
  11. {
  12. if($start ==1)
  13. {
  14. $sql = $sql . "and '". $_POST['name'] ."'";
  15. }
  16. else
  17. {
  18. $sql = $sql . $_POST['name'];
  19. }
  20. }
  21.  
  22. $sql = $sql . ";";
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #6
Jul 12th, 2007
I want elegant way to check and generate.

Now think that we have 15 fields to check. Is there an easy way to check ( loop maybe ) ?
Last edited by MitkOK; Jul 12th, 2007 at 9:40 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #7
Jul 12th, 2007
I don't know, i'll have to have a think about it. Perhaps using an associative array.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #8
Jul 12th, 2007
This I wrote a function for my purpose, it simple but works for me :

  1. function genQuery() {
  2.  
  3. $row_names = array("name","email","phone","title");
  4.  
  5. $post_values = array($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['title']);
  6.  
  7. $num = count($post_values);
  8.  
  9. $sqlString = "SELECT * FROM t WHERE";
  10.  
  11. for ($i=0;$i<$num;$i++) {
  12.  
  13. if (empty($post_values[$i])) { $sqlString.=""; }
  14.  
  15. if (($post_values[$i]) && ($i==0)) { $sqlString.=" $row_names[$i] LIKE '%$post_values[$i]%'"; }
  16.  
  17. if (($post_values[$i]) && ($i!=0)) { $sqlString.=" $row_names[$i] LIKE '%$post_values[$i]%'"; }
  18.  
  19. }
  20.  
  21. $sqlString = preg_replace("/%'/", "%' AND ", $sqlString);
  22. $len = strlen($sqlString);
  23. $len=$len-4;
  24. $sqlString = substr($sqlString, 0 , $len);
  25.  
  26. return $sqlString;
  27.  
  28. }
Last edited by MitkOK; Jul 12th, 2007 at 12:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #9
Jul 12th, 2007
Good solution.
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

 
0
  #10
Jul 12th, 2007
Hi.

I don't think it's good, but at least works form me.

- Mitko Kostov
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the PHP Forum


Views: 4883 | Replies: 10
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC