0

我想搜索一个数据库。我只想按名称搜索,然后只发布该名称的结果。如果出现类似的名称,我希望他们能够选择正确的名称。一旦选择了名称,我想显示结果。但我希望页面上的结果样式...这是我现在的代码..需要知道如何设置结果样式...以及如何查询具有相似名称的结果。

<?php
mysql_connect("local", "dbname", "pass") or die("Error connecting to database: ".mysql_error());
/*
    localhost - it's location of the mysql server, usually localhost
    root - your username
    third is your password

    if connection fails it will stop loading the page and display an error
*/

mysql_select_db("db") or die(mysql_error());
/* tutorial_search is the name of database we've created */


?>
<?php
$query = $_GET['query'];
// gets value sent over search form

$min_length = 3;
// you can set minimum length of the query if you want

if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

    $query = htmlspecialchars($query);
    // changes characters used in html to their equivalents, for example: < to &gt;

    $query = mysql_real_escape_string($query);
    // makes sure nobody uses SQL injection

    $raw_results = mysql_query("SELECT * FROM member
        WHERE (`Name` LIKE '%".$query."%')") or die(mysql_error());

    // * means that it selects all fields, you can also write: `id`, `title`, `text`
    // articles is the name of our table

    // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
    // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
    // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

    if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

        while($results = mysql_fetch_array($raw_results)){
        // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

            echo "<p><h3>".$results['Name']."</h3>".$results['Telephone']." <br>".$results['ProviderID']."</p>";
            // posts results gotten from database(title and text) you can also show id ($results['id'])
        }

    }
    else{ // if there is no matching rows do following
        echo "No results";
    }

}
else{ // if query length is less than minimum
    echo "Minimum length is ".$min_length;
}
?>
4

1 回答 1

1

try this using dropdown box so that you can find exact name related values...and next time please use mysqli()...because mysql() is deprecated

 <table width="100%"  align="center">
    <td valign="top" align="center" style="">
    <form name="form1" id="form1" method="post" action="search.php"> //change here
    <tr>
    <td >
    Select Name :
    </td>
    <td>
    <input type="text" name="query" /> 
    </td>   
    </tr>
    <tr><td>
    <input type="submit" name="submit" id="submit" value="FILTER" />
    </td>   
    </tr>
    </form>
    </table>

this will be your search.php

<table align="center" border="1" cellpadding="0" cellspacing="0" width="100%" bordercolor="#804000">
    <?php

    echo '<tr>
    <td><b>&nbsp;Name</b></td>
    <td><b>&nbsp;Telephone</b></td>
    <td><b>&nbsp;ProviderID</b></td>
    </tr>';
    $select="SELECT * FROM member WHERE name='".$_POST['query']."'"; // here input box name
    $query=mysql_query($select) or die($select);
    $numrow=mysql_num_rows($query);
    if($numrow != 0)
    {
    while($row=mysql_fetch_array($query))
    {   
    ?><tr align="left" >
    <td><b>&nbsp;<?php echo $row['name']; ?></b></td>
    <td><b>&nbsp;<?php echo $row['telephone']; ?></b></td>  
    <td ><b>&nbsp;<?php echo $row['ProviderID']; ?></b></td>    
    </tr><?php  } } else  { echo '<table ><tr align="center"  style="font-size:16px;line-height:150px;color:#FF0000;">
    <font color="#FF0000" >NO RECORDS FOUND</font>
    </tr></table>'; }  ?>
    </table>
于 2013-09-17T04:20:53.240 回答