www.webdeveloper.com
Results 1 to 4 of 4

Thread: how to concatenate strings before executing??

  1. #1
    Join Date
    May 2014
    Posts
    28

    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
    Posts
    51
    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.

    Suppose

    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
    Location
    Ankh-Morpork
    Posts
    19,337
    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();
    foreach(
    $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); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    May 2014
    Posts
    912
    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.

    Code:
    $statement = $db->prepare('
      SELECT * FROM table_name
      WHERE' . str_repeat(' skill regexp ?', count($skills))
    );
    
    $statement->execute($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 05:01 PM.
    Java is to JavaScript as Ham is to Hamburger.

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



Recent Articles