943,935 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 7418
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 12th, 2007
0

Generate sql string to query MySQL DB ?

Expand Post »
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.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

Do you just mean the sql statement?

Something like this...

$sql="select * from table where name = ' " . $_POST[name] . " ' ";
$sql = $sql . " and phone = ' " . $_POST[phone] . " ';";
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

Yes, that's right. But what if $_POST['name'] is not set ? And what if there are more fields to check ?

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

The previous post is wrong, I'm sorry.

I cannot edit it.
Last edited by MitkOK; Jul 12th, 2007 at 9:41 am.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

this is really rough but i think you mean something along these lines

PHP Syntax (Toggle Plain Text)
  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 . ";";
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

I want elegant way to check and generate.

Quote ...
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.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

I don't know, i'll have to have a think about it. Perhaps using an associative array.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

This I wrote a function for my purpose, it simple but works for me :

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

Good solution.
Reputation Points: 27
Solved Threads: 14
Junior Poster
Cerberus is offline Offline
162 posts
since Sep 2006
Jul 12th, 2007
0

Re: Generate sql string to query MySQL DB ?

Hi.

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

- Mitko Kostov
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: File Uploads
Next Thread in PHP Forum Timeline: If URL Exists





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC