Jump to content

change search from = to LIKE


mackin

Recommended Posts

Is it possible to change the code below so the search uses LIKE - so as not to be so exact with the results - I assume I need to put est_name LIKE.... but where do the % % go? if I put them around "text" the search doesnt work.

Thx in Advance

 

 

Stu

 

$colname_hotel_name_RS = "-1";
if (isset($_POST['name'])) {
  $colname_hotel_name_RS = $_POST['name'];
}


mysql_select_db($database_contractors, $contractors);
$query_hotel_name_RS = sprintf("SELECT * FROM hotels WHERE est_name = %s", GetSQLValueString($colname_hotel_name_RS, "text"));
$hotel_name_RS = mysql_query($query_hotel_name_RS, $contractors) or die(mysql_error());
$row_hotel_name_RS = mysql_fetch_assoc($hotel_name_RS);
$totalRows_hotel_name_RS = mysql_num_rows($hotel_name_RS);

Link to comment
Share on other sites

I think the SQL should look like this (I belive you need to escape the %'s used for the Like as you are using a sprintf to build the string):

"SELECT * FROM hotels WHERE est_name LIKE '\%%s\%'", GetSQLValueString($colname_hotel_name_RS, "text")

 

if that errors out try it without the \'s at the %'s and if that still doesn't work post us up your error messages.

Link to comment
Share on other sites

Hi Keith, when I use your code I get this error

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%'abbey'%' at line 1

 

That is with a search for abbey

 

muddy - same search with code verbatim gives You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''\%s\' at line 1

removing the escaping gives

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''%s' at line 1

 

Cheers people

Link to comment
Share on other sites

try this:

$colname_hotel_name_RS = "-1";

if (isset($_POST['name'])) {

  $colname_hotel_name_RS = $_POST['name'];

}

 

 

mysql_select_db($database_contractors, $contractors);

$query_hotel_name_RS = sprintf("SELECT * FROM hotels WHERE est_name LIKE '\%%s\%'", GetSQLValueString($colname_hotel_name_RS, "text"));

print_r($query_hotel_name_RS);

exit();

$hotel_name_RS = mysql_query($query_hotel_name_RS, $contractors) or die(mysql_error());

$row_hotel_name_RS = mysql_fetch_assoc($hotel_name_RS);

$totalRows_hotel_name_RS = mysql_num_rows($hotel_name_RS);

Link to comment
Share on other sites

$colname_hotel_name_RS = "-1";
if (isset($_POST['name'])) {
  $colname_hotel_name_RS = $_POST['name'];
}


mysql_select_db($database_contractors, $contractors);
$query_hotel_name_RS = sprintf("SELECT * FROM hotels WHERE est_name LIKE '\%%s\%'", GetSQLValueString($colname_hotel_name_RS, "text"));
print_r($query_hotel_name_RS);
exit();
$hotel_name_RS = mysql_query($query_hotel_name_RS, $contractors) or die(mysql_error());
$row_hotel_name_RS = mysql_fetch_assoc($hotel_name_RS);
$totalRows_hotel_name_RS = mysql_num_rows($hotel_name_RS);

 

That gives me

 

SELECT * FROM hotels WHERE est_name LIKE '\%s\ - with same abbey search term

Link to comment
Share on other sites

Hi

 

Try this:-

 

$colname_hotel_name_RS = "-1";
if (isset($_POST['name'])) {
  $colname_hotel_name_RS = $_POST['name'];
}


mysql_select_db($database_contractors, $contractors);
$query_hotel_name_RS = sprintf("SELECT * FROM hotels WHERE est_name = %s", '%'.GetSQLValueString($colname_hotel_name_RS, "text").'%'); 
echo "$query_hotel_name_RS";
$hotel_name_RS = mysql_query($query_hotel_name_RS, $contractors) or die(mysql_error());
$row_hotel_name_RS = mysql_fetch_assoc($hotel_name_RS);
$totalRows_hotel_name_RS = mysql_num_rows($hotel_name_RS); 

 

See what the SQL is that it outputs

 

All the best

 

Keith

Link to comment
Share on other sites

Hi

 

Try this:-

 

$colname_hotel_name_RS = "-1";
if (isset($_POST['name'])) {
  $colname_hotel_name_RS = $_POST['name'];
}


mysql_select_db($database_contractors, $contractors);
$query_hotel_name_RS = sprintf("SELECT * FROM hotels WHERE est_name = %s", '%'.GetSQLValueString($colname_hotel_name_RS, "text").'%'); 
echo "$query_hotel_name_RS";
$hotel_name_RS = mysql_query($query_hotel_name_RS, $contractors) or die(mysql_error());
$row_hotel_name_RS = mysql_fetch_assoc($hotel_name_RS);
$totalRows_hotel_name_RS = mysql_num_rows($hotel_name_RS); 

 

See what the SQL is that it outputs

 

All the best

 

Keith

gives me this keith

 

SELECT * FROM hotels WHERE est_name = %'abbey'%You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%'abbey'%' at line 1

Link to comment
Share on other sites

Hi

 

Ah, right. Seem what is confusing me. GetSQLValueString returns a string with inverted commas already in place.

 

Try this:-

 

$colname_hotel_name_RS = "-1";
if (isset($_POST['name'])) {
  $colname_hotel_name_RS = $_POST['name'];
}


mysql_select_db($database_contractors, $contractors);
$query_hotel_name_RS = sprintf("SELECT * FROM hotels WHERE est_name = %s", GetSQLValueString('%'.$colname_hotel_name_RS.'%', "text")); 
echo "$query_hotel_name_RS";
$hotel_name_RS = mysql_query($query_hotel_name_RS, $contractors) or die(mysql_error());
$row_hotel_name_RS = mysql_fetch_assoc($hotel_name_RS);
$totalRows_hotel_name_RS = mysql_num_rows($hotel_name_RS);  

 

All the best

 

Keith

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.