-1

如何搜索确切的数字

这段代码是这样工作的

如果我搜索 12345678(这是完全不),它什么也没有显示

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

所以它显示这样的结果

像这样

12345678
12345678
12348591
12346899
12345698

我想要这样的结果

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

所以它就像这样显示

12345678
12345678

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

这是代码

<?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

1 回答 1

1

要搜索您需要删除的确切数字LIKE,请更改:

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

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

您通过执行以下操作将结果循环 2 次:

while ($runrows = mysql_fetch_assoc($run))
..
if ($runrows > 0) {
        while($runrows = mysql_fetch_array($run)) {

只需将其更改为:

if ($runrows > 0) {
        while($runrows = mysql_fetch_array($run)) {
于 2013-10-22T04:24:55.150 回答