Search code examples
phpmysqlfieldsql-like

LIKE operator and correct usage with NOT IN


Possible Duplicate:
mysql_fetch_array() expects parameter 1 to be resource, boolean given in select

This currently works...

$resultInput = mysql_query("SHOW COLUMNS FROM " . $table. " WHERE Field NOT IN ('id', 'created', 'date_modified', 'content', 'type', 'bodytext', 'project_content', 'content_short') AND Field NOT LIKE '%_image%'");

However I would like to remove all the fields with the name content and add it to the LIKE function with something like %content%

$resultInput = mysql_query("SHOW COLUMNS FROM " . $table. " WHERE Field NOT IN ('id', 'created', 'date_modified', 'type', 'bodytext') AND Field NOT LIKE ('%_image%', %content%)");

But this doesn`t seem to work? and returns a"

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in
C:\xampp\htdocs\framework.php on line 33


Solution

  • What you are attempting is actually a syntax error. LIKE conditions must be individually listed and separated by AND or OR. In your case however, you can do it with REGEXP by separating the conditions with |:

    SHOW COLUMNS 
    FROM " . $table. " 
    WHERE Field NOT IN ('id', 'created', 'date_modified', 'type', 'bodytext') 
    AND Field NOT REGEXP '_image|content'
    

    Because the regular expression will match the patterns content or _image anywhere inside Field, there is no need for any additional equivalent characters to the % wildcards used in LIKE.

    We assume that $table has been compared against a list of valid table names to use in this query for security purposes.

    Finally, you received the fatal error mysql_num_rows() expects parameter 1 to be resource because you performed no error checking on the query result.

    $result_input = mysql_query("SHOW COLUMNS.....");
    if (!$result_input) {
      // Something's wrong
      echo mysql_error();
    }
    else {
      // ok, everything is working, proceed.
    }