Results 1 to 4 of 4

Thread: how to concatenate strings before executing??

  1. #1
    Join Date
    May 2014

    resolved how to concatenate strings before executing??

    i have an array of skills like skills1,skills2,skills3 etc etc.

    i wanna make a query like

    select * from tablenaame where skills regexp "skills1" and skills regexp "skills2" and skills regexp "skills3";

    how can i do this withour errors??

  2. #2
    Join Date
    Mar 2014
    I can do this thing in Python very easily. First declare a variables to the number of strings you want to concatenate and then assign each variable a string after that you can call your variables with adding a plus sign.


    var1 = "happy"
    var2 = "mind"
    var3 = "healthy mind"

    print var1 + var2 + var3

    It was a python code, you can have some idea from here and yes you are saying it should be before concatenate then its little different.

  3. #3
    Join Date
    Aug 2004
    At one level, I'm concerned about multiple regexp comparisons, as they will require full table reads, so I'm wondering if you might be better off with a FULLTEXT index/search? If you do need to do it the way you've described, I'll typically build another array of search expressions that can then be glued together, e.g.:
    PHP Code:
    $search = array();
    $skills as $skill) {
    $search[] = "skill regexp '$skill'"// but make sure you have sanitized $skill for your database extension!!!
    $sql "SELECT * FROM table_name WHERE ".implode(' AND '$search); 
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    I agree that regex is a bad idea -- so is blindly plugging in values to a query. If you were using PDO or mysqli, you can simply str_repeat a "?" pass in the query equal to the number of values in your $skills array.

    $statement = $db->prepare('
      SELECT * FROM table_name
      WHERE' . str_repeat(' skill regexp ?', count($skills))
    Simpler, smaller, cleaner, faster. I really like PDO... especially since doing a prepare/exec means the values are sanitized ahead of time.
    Last edited by deathshadow; 05-29-2014 at 06:01 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center