PHP搜索和数据库结果

时间:2013-09-17 03:31:25

标签: php

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

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

1 个答案:

答案 0 :(得分:1)

使用下拉框尝试此操作,以便您可以找到确切的名称相关值...下次请使用mysqli()...因为不推荐使用mysql()

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

这将是你的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>