如何在php中搜索确切的数字

时间:2013-10-21 15:09:20

标签: php mysql search

如何搜索确切的数字

这段代码就像这样工作

如果我搜索24626838(这是完全没有)它没有显示任何内容

之后如果我搜索这个没有2462683(这不是完整的否)

所以它显示了这样的结果

像这样

24626838
24626838
24626836
24626832
24626837

我希望得到像这样的结果

如果我搜索这个没有24626838(这是完整的否)

所以它显示的确如此

24626838
24626838

我该怎么做才能帮我解决这个问题

,这是代码

<?php

    //connect to database

     mysql_connect('localhost','root','');
     mysql_select_db('member');

$alphabet = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'); 
foreach ($alphabet as $letter) { 
echo "<a href=\"viewuser.php?letter=" . $letter . "\" class=\"style2\">" . $letter . "</a>&nbsp;|&nbsp;"; 

} 


echo "<br>";
echo "<a href=\"index.php?\" class=\"style2\">Show All</a></p><br /> <form method=\"post\" action=\"?\">

        <input type=\"hidden\" name=\"letter\" value=\"{$_GET['letter']}\" />";


$page = (empty($_GET['page'])) ? 1 : $_GET['page'];
$letter = (empty($_GET['letter'])) ? "%" : $_GET['letter'];
$name = (empty($_GET['name'])) ? "%male" : $_GET['name'];
?></td>
            </tr>

            <p> 
              <?php

//get date
$button = $_GET['submit'];
$search = $_GET['search'];

$s = $_GET['s'];
if (!$s)

$s = 0;
$e = 12;

$next = $s + $e; $prev = $s - $e;
    if (strlen($search)<=1)
   echo "";

    else
   {
     echo "<class=\"style2\"> You searched for <b>$search</b>";

     //connect to database

     mysql_connect('localhost','root','');
     mysql_select_db('member');



 //explode search term
           $search_exploded = explode(" ",$search);
           foreach($search_exploded as $search_each)
{
    $str = mysql_real_escape_string(substr($search_each, 0, 9));
    //construct query
    $x++;
    if ($x==1) $construct .= "number LIKE '$str%'";

}




        //echo out construct

  $construct = "SELECT * FROM workorder WHERE $construct";

    $run = mysql_query($construct);
     $foundnum = mysql_num_rows($run);
       if ($foundnum==0)
        echo "<class=\"style2\">No user found.";
     else
     {






 ?>






<?php

 echo "<class=\"style2\"><p>$foundnum results found.</p>";

       while ($runrows = mysql_fetch_assoc($run))
              {
echo "<table class='hovertable' border='1' cellpadding='0' cellspacing='0'>";
echo "<tr><th>User name</th><th>Number</th><th>Exchange</th><th>Cabnint</th><th>Service</th><th>Open Date</th><th>Close Date</th><th>Remaks</th><th>Status</th><th>Edit</th><th>Detail</th><th>Delete</th></tr>";

if ($runrows > 0) {
        while($runrows = mysql_fetch_array($run)) {
                echo "<tr><td>";
             echo $runrows['username'];
            echo "</td><td>";
                echo $runrows['number'];
                echo "</td><td>";
            echo $runrows['exchange'];
        echo "</td><td>";
        echo $runrows['cabnint'];
                echo "</td><td>";
        echo $runrows['service'];
                echo "</td><td>";
        echo $runrows['opendate'];
                echo "</td><td>";
        echo $runrows['closedate'];
                echo "</td><td>";
        echo $runrows['remaks'];
                echo "</td><td>";
        echo $runrows['status'];
                echo "</td><td>";
            echo '<a href="edit.php?id=' . $runrows['id'] . '" class="style2">Edit</a>';
        echo "</td><td>";
            echo '<a href="detail.php?id=' . $runrows['id'] . '" class="style2">Detail</a>';
        echo "</td><td>";
            echo '<a href="delete.php?id=' . $runrows['id'] . '" class="style2">Delete</a>';
                echo "</td></tr>";

        }
} else {
        echo "<tr><td colspan=\"5\">No results found!</td></tr>";
}

echo "</table>";
       }

     }



    }
?>

3 个答案:

答案 0 :(得分:0)

如果您要搜索确切的数字,请使用column='$number'代替column LIKE $number。在WHERE子句中,我建议将偶数括在单引号中,因为我最近遇到的问题是使用WHERE column = 1234这也与12345匹配,但WHERE column = '1234'匹配只有非常确切的数字。

答案 1 :(得分:0)

我想问题出在这里:

number LIKE '$str%'

不要那样用。相反,尝试这样:

number = '$str'

因此它执行严格的SELECT而不是搜索。当你给%时,可能会有其他东西存在(或者不存在,我不确定)。

此外,将LIKE用于数字并不是一个好主意。这只是一个建议。

答案 2 :(得分:0)

应该可以试试这个

 if ($x==1) $construct .= "number LIKE '".$str."%'";