I have a simple array named j12stuff that is a list of words. Can I use one mysql query to select all words from a table that matches the words in the array that are nouns? The point is to use mysql to reduce the array list to nouns only.

The table columns are word and ss_type. ss_type is n for noun.

Thanks for your help,
crazyjeremy

    //Populate your j12stuff array.
    $j12stuff = array('word1', 'word2');
    
    //Set a base query.
    $query = 'SELECT * FROM `some_table` WHERE ((`word` = "filler" AND `ss_type` = "filler")';
    
    foreach ($j12stuff as $word) {
    	$query .= ' OR (`word` = "' . $word . '" AND ss_type = "n")';
    }
    
    //Close the query.
    $query .= ')';
    
    echo $query;
    

      This is 100% working now. I guess I could have done it this way from the beginning but my original intention was to find out if mysql had any inherant ability to accept a php array. If this is not the case, please say so and we can resolve the thread. If mysql can accept an array directly, how would it work?

        Hi,

        you can do something like this:

        $j12stuff = array('word1','word2','word3');
        
        $query  = "SELECT * FROM theTable WHERE ss_type='n' AND word IN ('";
        $query .= implode("','",$j12stuff);
        $query .= "')";
        

        Don't forget to escape single quotes in the array members. You might run into troubles if the array is too large because MySQL has a limit regarding the length of a query string.

        Regards,
        Thomas

          Write a Reply...