Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> include('config.php'); $searchfields = array('name', 'cat1', 'cat2', 'cat3', ) $cleandata = mysql_real_escape_string(trim(strtolower($_REQUEST['search']))); $where = array(); foreach($searchfields as $field) { $where[] = 'lcase('.$field.') like \'%.$cleandata.%\''; } $result = mysql_query('SELECT * FROM gj WHERE '.implode(' OR ', $where).' ORDER by name') or trigger_error(mysql_error()); $num_rows = mysql_num_rows($result); </code></pre> <p>I've added a variable cleandata which contains the cleaned request data (to prevent SQL injection).</p> <p>I've created a variable searchfields which can be easily expanded to search on more fields.</p> <p>To search on more fields simply expand the array data:</p> <pre><code> $searchfields = array('name', 'cat1', 'cat2', 'cat3', 'cat4', ) </code></pre> <p>EDIT Added case insensitive</p> <hr> <p><em>EDIT</em> Added PDO</p> <p>Note that since I have written this answer the community has begun the <a href="http://goo.gl/KJveJ" rel="nofollow">deprecation process</a> of the ancient <code>mysql_*</code>. See the <a href="http://goo.gl/GPmFd" rel="nofollow"><strong>red box</strong></a>? Instead you should learn about <a href="http://goo.gl/vn8zQ" rel="nofollow">prepared statements</a> and use either <a href="http://php.net/pdo" rel="nofollow">PDO</a> or <a href="http://php.net/mysqli" rel="nofollow">MySQLi</a>. If you can't decide, <a href="http://goo.gl/3gqF9" rel="nofollow">this article</a> will help to choose. If you care to learn, <a href="http://goo.gl/vFWnC" rel="nofollow">here is a good PDO tutorial</a>.</p> <p>A rewrite of the above answer using PDO would look like something like the following:</p> <pre><code>$dbConnection = new PDO('mysql:dbname=dbtest;host=127.0.0.1;charset=utf8', 'user', 'pass'); $dbConnection-&gt;setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $dbConnection-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $searchfields = array('name', 'cat1', 'cat2', 'cat3', ) $where = array(); foreach($searchfields as $field) { $where[] = 'lcase(' . $field . ') LIKE :keyword'; } $stmt = $dbConnection-&gt;prepare('SELECT * FROM gj WHERE ' . implode(' OR ', $where) . ORDER BY name); $stmt-&gt;execute(array( ':keyword' =&gt; '%' . trim(strtolower($_REQUEST['search'])) . '%', )); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload