#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Passing Array of Words to MySQL Query


    Hello,

    I have a list of words and I would like to query each one of those words against a mySQL text column and count the results. I don't need to count individual occurrences of that word in each row, just if the word is IN the row.

    I THINK it would go something like:

    Set variable to String of Keywords
    EXPLODE String into Array
    FOR EACH keyword, query column using COUNT
    RETURN Keyword and Count

    PHP Code:
    $keywords "keyword1, keyword2, keyword3";
    $seperatekeywords explode(","$keywords);

    foreach (
    seperatekeywords as $keyterm){

    $result mysql_query("SELECT COUNT(column) as termCount FROM table WHERE column LIKE CONCAT('%', $keyterm, '%')");

    echo 
    $result;

    I'm getting blank with no errors....
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,847
    Rep Power
    6351
    This is going to be slow, unfortunately. You can do it with one query for every word, or a slightly more complicated set of IF conditions.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    I think I got it to work:

    PHP Code:
    $keywords "keyword1, keyword2, keyword3";
    $words explode(","$keywords);

    foreach (
    $words as $keyterm){

    $result mysql_query("SELECT  COUNT(Column) from Table WHERE column LIKE '%keyterm%'");

    While(
    $row mysql_fet_array($result)){
    echo 
    $keyterm;
    echo 
    $row['COUNT(Column)'];
    }

  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,847
    Rep Power
    6351
    LIKE is case-sensitive, so "London" won't match "london."

    Also, mysql_fet_array isn't a function.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo