User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,559 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,743 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1904 | Replies: 10 | Solved
Reply
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation: MitkOK is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Generate sql string to query MySQL DB ?

  #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 6:47 am.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

  #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  
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation: MitkOK is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

  #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 7:40 am.
Reply With Quote  
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation: MitkOK is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

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

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

Re: Generate sql string to query MySQL DB ?

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

$sql = "select * from table name where ";
$start;

if(isset $_POST['name'])
{
    $sql = $sql . $_POST['name'];
    $start = 1;
}

if(isset $_POST['phone'])
{
    if($start ==1)
    {
        $sql = $sql . "and '". $_POST['name'] ."'";
    }
    else
    {
        $sql = $sql . $_POST['name'];
    }
}

$sql = $sql . ";";
Reply With Quote  
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation: MitkOK is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

  #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 8:40 am.
Reply With Quote  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

  #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  
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation: MitkOK is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

  #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 11:13 am.
Reply With Quote  
Join Date: Sep 2006
Posts: 162
Reputation: Cerberus is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 14
Cerberus Cerberus is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

  #9  
Jul 12th, 2007
Good solution.
Reply With Quote  
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation: MitkOK is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 10
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Generate sql string to query MySQL DB ?

  #10  
Jul 12th, 2007
Hi.

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

- Mitko Kostov
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb PHP Marketplace
Thread Tools Display Modes

Other Threads in the PHP Forum

All times are GMT -4. The time now is 9:59 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC