Page 1 of 1

How can i add another column to search?

Posted: Mon Sep 20, 2010 9:16 am
by kevrelland
I have sorted this autocompleter but i can only get it to search one column and i want it to search two
This works

Code: Select all

<?php

$q = strtolower( $_GET["q"] );
if (!$q) return;

$dbhost = "localhost";		// Database Host
$dbuser = "root";			// User
$dbpass = "";			// Password
$dbname = "shop";			// Name of Database

mysql_connect( $dbhost, $dbuser, $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname ) or die( mysql_error() );

// Replace "TABLE_NAME" below with the table you'd like to extract data from
$data = mysql_query( "SELECT * FROM tbl_products" )
or die( mysql_error() );

// Replace "COLUMN_ONE" below with the column you'd like to search through
// In between the if/then statement, you may present a string of text
// you'd like to appear in the textbox.
while( $row = mysql_fetch_array( $data )){
	if ( strpos( strtolower($row['product_Description'] ), $q ) !== false ) {
		echo $row['product_Name'] . "\n";
	}
}

?>
but i tried this and it doesn't work at all

Code: Select all

<?php

$q = strtolower( $_GET["q"] );
if (!$q) return;

$dbhost = "localhost";		// Database Host
$dbuser = "root";			// User
$dbpass = "";			// Password
$dbname = "shop";			// Name of Database

mysql_connect( $dbhost, $dbuser, $dbpass ) or die( mysql_error() );
mysql_select_db( $dbname ) or die( mysql_error() );

// Replace "TABLE_NAME" below with the table you'd like to extract data from
$data = mysql_query( "SELECT * FROM tbl_products" )
or die( mysql_error() );

// Replace "COLUMN_ONE" below with the column you'd like to search through
// In between the if/then statement, you may present a string of text
// you'd like to appear in the textbox.
while( $row = mysql_fetch_array( $data )){
	if ( strpos( strtolower($row['product_Description']) || strtolower($row['product_Name']) , $q ) !== false ) {
		echo $row['product_Name'] . "\n";
	}
}

?>
Any ideas
Cheers
Kevin

Re: How can i add another column to search?

Posted: Mon Sep 20, 2010 3:45 pm
by McInfo
home-alone.jpg
home-alone.jpg (2.19 KiB) Viewed 115 times
Do not use PHP to filter query results!

Use SQL.

Code: Select all

SELECT `col1`, `col2`, `col3` FROM `table` WHERE `col1` LIKE '%term%' OR `col2` LIKE '%term%'