如何用PHP做一个查询功能和搜索功能....急!!!!!在线等.

解决方案 »

  1.   

    前台html
    添加一个<input type="text" name="keyword">的表单抓关键字把抓到的关键字 构造sql  抓取数据库 用like匹配select * from `table` where `keyword` like `%$keyword%`;注意%是通配符 , 表示若干字符例:
    table 
    id     value
    1      abc
    2      abcd
    3      bcd
    4      bcde$keyword = "bcd";select * from `table` where `keyword` like '$keyword';
    匹配到id 3select * from `table` where `keyword` like '%$keyword';
    匹配到id 2 , 3select * from `table` where `keyword` like '$keyword%';
    匹配到id 3 , 4select * from `table` where `keyword` like '%$keyword%';
    匹配到id 2 , 3 , 4
      

  2.   

    网上关于通配符的资料通配符 说明 
    _    与任意单字符匹配
     
    %    与包含一个或多个字符的字符串匹配
     
    [ ]  与特定范围(例如,[a-f])或特定集(例如,[abcdef])中的任意单字符匹配。
     
    [^]  与特定范围(例如,[^a-f])或特定集(例如,[^abcdef])之外的任意单字符匹配。
     例子:• WHERE FirstName LIKE '_im' 可以找到所有三个字母的、以 im 结尾的名字(例如,Jim、Tim)。 
     
    • WHERE LastName LIKE '%stein' 可以找到姓以 stein 结尾的所有员工。 
     
    • WHERE LastName LIKE '%stein%' 可以找到姓中任意位置包括 stein 的所有员工。 
     
    • WHERE FirstName LIKE '[JT]im' 可以找到三个字母的、以 im 结尾并以 J 或 T 开始的名字(即仅有 Jim 和 Tim) 
     
    • WHERE LastName LIKE 'm[^c]%' 可以找到以 m 开始的、后面的(第二个)字母不为 c 的所有姓。
      

  3.   

    就是where条件
    难点的话就是关于比对 统配
      

  4.   

    /* 仿真 Adodb 函数 */
        function selectLimit($sql, $num, $start = 0)
        {
            if ($start == 0)
            {
                $sql .= ' LIMIT ' . $num;
            }
            else
            {
                $sql .= ' LIMIT ' . $start . ', ' . $num;
            }        return $this->query($sql);
        }    function getOne($sql, $limited = false)
        {
            if ($limited == true)
            {
                $sql = trim($sql . ' LIMIT 1');
            }        $res = $this->query($sql);
            if ($res !== false)
            {
                $row = mysql_fetch_row($res);            if ($row !== false)
                {
                    return $row[0];
                }
                else
                {
                    return '';
                }
            }
            else
            {
                return false;
            }
        }    function getOneCached($sql, $cached = 'FILEFIRST')
        {
            $sql = trim($sql . ' LIMIT 1');        $cachefirst = ($cached == 'FILEFIRST' || ($cached == 'MYSQLFIRST' && $this->platform != 'WINDOWS')) && $this->max_cache_time;
            if (!$cachefirst)
            {
                return $this->getOne($sql, true);
            }
            else
            {
                $result = $this->getSqlCacheData($sql, $cached);
                if (empty($result['storecache']) == true)
                {
                    return $result['data'];
                }
            }        $arr = $this->getOne($sql, true);        if ($arr !== false && $cachefirst)
            {
                $this->setSqlCacheData($result, $arr);
            }        return $arr;
        }    function getAll($sql)
        {
            $res = $this->query($sql);
            if ($res !== false)
            {
                $arr = array();
                while ($row = mysql_fetch_assoc($res))
                {
                    $arr[] = $row;
                }            return $arr;
            }
            else
            {
                return false;
            }
        }
      

  5.   

    在php 5.2 中文windows xp通过,如果显示乱码,请在php.ini中设置
    default_charset = "gbk"<html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=gbk" />
    </head>
    <body>
    <form method="POST">
    <table>
    <tr>
    <td>keyword:</td><td><input type="text" size="20" value="<?php echo isset($_REQUEST["kw"])?$_REQUEST["kw"]:""; ?>" name="kw"></td><td><input type="submit" name="go!" value="submit"></td>
    </tr></table>
    </form>
    <?php
    echo "<pre>";
    if(isset($_REQUEST["kw"]))
    {
    $targetdir = "./txt";$dir=dir($targetdir);
    //rename usage
    while($entry=$dir->read())
    {
    if($entry == "." || $entry == "..") continue;if(strstr($entry,$_REQUEST["kw"]))
    {
    $string = file_get_contents($entry);
    echo $string."\n"; 
    break;
    }
    }
    echo "没有这个文件!";
    }
    ?></body>
    </html>