0

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.

8
  • 5
    BETWEEN doesn't work like the LIKE command. You can't use the wildcard (%) characters. Also, obligatory sql injection warning. Commented Dec 2, 2013 at 18:33
  • Then what is the solution for this code? Commented Dec 2, 2013 at 18:34
  • dev.mysql.com/doc/refman/5.0/en/… Commented Dec 2, 2013 at 18:36
  • SQL Injection warning not appropriate - OP has escaped the user input in the first snippet. Commented Dec 2, 2013 at 18:36
  • 1
    Try removing the wildcard characters. Commented Dec 2, 2013 at 18:37

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.