Hello. I'm trying to use a wildcard for a $var. in a SELECT statement.
Can't seem to get it to work. '$search_Recordset2' is the $var.

("SELECT * FROM table WHERE col1 LIKE '$search_Recordset2' OR col2 LIKE '$search_Recordset2' OR col3 LIKE '$search_Recordset2'"

If I use '%$search_Recordset2%' or %'$search_Recordset2'%
I get error.

Recommended Answers

All 6 Replies

What is the error. Do you get the error in PHP or in MySQL?

Warning: sprintf() [function.sprintf]: Too few arguments in C:\xampp\htdocs\folder\search.php on line 62
Query was empty

Try this code and see what error you are getting.

<?
	echo $query= "SELECT * FROM table WHERE col1 LIKE '%$search_Recordset2%' OR col2 LIKE '%$search_Recordset2%' OR col3 LIKE '%$search_Recordset2%'";	
	$result = mysql_query($query);
	if (!$result) {
		die('Invalid query: ' . mysql_error());
	}

?>
Member Avatar for diafol

Are you using sprintf? If so it will look for the '%' to replace it.

Warning: sprintf() [function.sprintf]: Too few arguments in C:\xampp\htdocs\folder\search.php on line 62
Query was empty

Are you using a PHP framework? Or a custom database class?

Whats happening is that your SQL query is being run through sprintf() function, which will try and replace the special keys that start with the '%' signs with the variables passed in. See: http://php.net/sprintf

Thanks everyone, I tried this and it seemed to work ok.

1.
      ("SELECT * FROM table WHERE col1 LIKE '%%$search_Recordset2%%' OR col2 LIKE '%%$search_Recordset2%%' OR col3 LIKE '%%$search_Recordset2%%'"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.