1

如何搜索确切的数字

这段代码是这样工作的

如果我搜索 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>";
       }

     }



    }
?>
4

3 回答 3

0

它应该工作试试这个

 if ($x==1) $construct .= "number LIKE '".$str."%'";
于 2013-10-22T04:35:35.243 回答
0

如果要搜索确切的数字,请使用column='$number'而不是column LIKE $number. 在WHERE子句中,我建议用单引号将偶数括起来,因为我最近遇到了使用WHERE column = 1234this 也匹配的问题12345,但WHERE column = '1234'只匹配非常精确的数字。

于 2013-10-21T15:12:28.893 回答
0

我想问题出在这里:

number LIKE '$str%'

不要用那种方式。相反,请尝试以这种方式提供:

number = '$str'

这样它就执行严格SELECT而不是搜索。当您给出 a%时,它可能会期望出现其他内容(或不存在,我不确定这一点)。

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

于 2013-10-21T15:12:48.517 回答