WeCenter 学习笔记--用户搜索功能

搜索

  • 分析
    (主要是处理数组数据的问题)

    1. eg: $q = array("Volvo","BMW","Toyota");
      输入数据为数组 且 数组元素大于一个的时候 ,
      where[] = user_name= 'Volvo BMW Toyota' OR user_name = 'VolvoBMWToyota'

    2. eg: $q = array("Volvo");
      输入数据为数组 且 数组元素等于一个的时候 ,
      where[] = user_name= 'Volvo%'

    3. eg: $q = "Volvo"
      输入数据为不为数组 的时候 ,
      where[] = user_name= 'Volvo%'

  • 代码
    public function search_users($q, $page, $limit = 20)
    {
        if (is_array($q) AND sizeof($q) > 1)
        {
            $where[] = "user_name = '" . $this->quote(implode(' ', $q)) . "' OR user_name = '" . $this->quote(implode('', $q)) . "'";
        }
        else
        {
            if (is_array($q))
            {
                $q = implode('', $q);
            }

            $where[] = "user_name LIKE '" . $this->quote($q) . "%'";
        }

        return $this->query_all('SELECT uid, last_login FROM ' . get_table('users') . ' WHERE ' . implode(' OR ', $where), calc_page_limit($page, $limit));
    }

quote过滤函数

  • 代码
    /**
     * 添加引号防止数据库攻击
     *
     * 外部提交的数据需要使用此方法进行清理
     *
     * @param   string
     * @return  string
     */
    public function quote($string)
    {
        if (is_object($this->db()))
        {
            $_quote = $this->db()->quote($string);

            if (substr($_quote, 0, 1) == "'")
            {
                $_quote = substr(substr($_quote, 1), 0, -1);
            }

            return $_quote;
        }

        if (function_exists('mysql_escape_string'))
        {
            $string = @mysql_escape_string($string);
        }
        else
        {
            $string = addslashes($string);
        }

        return $string;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值