Subhajit
Subhajit

Reputation: 398

PHP search using BETWEEN not working

I create a two field that search between two entries. by using GET method.

<form name="search">
  <input name="to" id="to" type="text" />
  <input name="end" id="end" type="text" />
  <input name="submit" id="submit" type="submit" />
</form>
<table border="1">
  <tr>
    <td>Name</td>
  </tr>
<?php

if(isset($_GET['submit'])){

$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="administrator";
$db_tb_name="customer_details";
$db_tb_usr_name="name";
$db_tb_npkgr_name="no_of_pkg";

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$s_name=mysql_real_escape_string($_GET['to']);
$m_name=mysql_real_escape_string($_GET['end']);

$query_for_result = mysql_query("SELECT * FROM $db_tb_name WHERE 
$db_tb_npkgr_name BETWEEN '%".$s_name."%' AND '%".$m_name."%'");

while($data_fetch=mysql_fetch_array($query_for_result))
{
?>
  <tr>
    <td><?php echo substr($data_fetch[$db_tb_usr_name], 0,160); ?></td>
  </tr>
<?php } mysql_close(); }?>
</table>

In this code the output showing nothing, but when I use one field for search then it work properly.

here is the working code for single search. I use here LIKE operator.

<form name="search">
  <input name="to" id="to" type="text" />
  <input name="submit" id="submit" type="submit" />
</form>
<table border="1">
  <tr>
    <td>Name</td>
  </tr>
<?php

if(isset($_GET['submit'])){

$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="administrator";
$db_tb_name="customer_details";
$db_tb_usr_name="name";
$db_tb_npkgr_name="no_of_pkg";

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$s_name=mysql_real_escape_string($_GET['to']);

$query_for_result = mysql_query("SELECT * FROM $db_tb_name WHERE 
$db_tb_npkgr_name LIKE '%".$s_name."%'");


while($data_fetch=mysql_fetch_array($query_for_result))
{
?>
  <tr>
    <td><?php echo substr($data_fetch[$db_tb_usr_name], 0,160); ?></td>
  </tr>
<?php }
mysql_close(); }?>
</table>

This code work great. But when I use BETWEEN then it shows nothing in output. What is the wrong? What is the solution?

Thank you.

Upvotes: 0

Views: 75

Answers (1)

Jonathan Kuhn
Jonathan Kuhn

Reputation: 15301

BETWEEN doesn't work like the LIKE command. You can't use the wildcard (%) characters. Try removing them.

Upvotes: 3

Related Questions