dragonsun00000 2014-07-15 06:44
浏览 51
已采纳

如果在php类中建立连接并且在单独的函数文件中使用函数mysqli_real_escape_string,我如何使用mysql连接?

I am working on a project in which I am using mysqli extension. I have made mysql connection in a PHP class called Connect.class.php like this -

// connect to db
    public function connect()
    {
        $this->con = @mysqli_connect($this->db_host, $this->db_username, $this->db_password, $this->db_name) or die("Couldn't connect! " . mysqli_connect_error());
        return $this->con;
    }

And I have one another file called functions.php in which I am sanitizing the input data like this -

// filter user input data
function filter_data($input)
{
    $sanitized_data = mysqli_real_escape_string(connection, htmlspecialchars(trim($input), ENT_QUOTES, "UTF-8"));
    return $sanitized_data;
}

Now the above code will not work since it has not correct connection parameter. I am not understanding how can I provide it proper connection that is made from my PHP class Connect.class.php. Since the property of an object can not be used in a function of a separate file i.e. i cant use $this->con.

If I use simple mysql in place of mysqli then there is no problem at all since mysql_real_escape_string() doesn't need connection as a necessary parameter. So what to do in this situation?

  • 写回答

1条回答 默认 最新

  • duanrang2627 2014-07-15 07:01
    关注

    You can do it by more than one way :

    1.1- Try putting the escape function inside the connect class, inside a function.

    //Make a function _escape($str) and put mysqli_real_escape_string($this->con,$str) in it
    //Connect.class.php
    public function _escape($str){
         return mysqli_real_escape_string($this->con, htmlspecialchars(trim($input), ENT_QUOTES, "UTF-8"));
    }
    

    1.2.1 (Sub method 1) Create an object of connect inside the function and run the escape method

    // filter user input data
    function filter_data($input)
    {
        $db = new Connect();
        $db->connect();
        return $db->_escape($input);
    }
    

    1.2.2 (Sub method 2) Just run the escape method outside filter_data, don't use this function any more

    $db = new Connect();
    $db->connect();
    $filtered = $db->_escape($input);
    

    2.1 Use the same approach as in one but don't put the escape function inside connect just make an extended class(Example dbFilter) and put it in there

    3.1 Use the connection object return by connect function

    // altered filter user input data
    function filter_data($input, $conn)
    {
        $sanitized_data = mysqli_real_escape_string($conn, htmlspecialchars(trim($input), ENT_QUOTES, "UTF-8"));
        return $sanitized_data;
    }
    
    //call it
    $db = new Connect();
    $conn = $db->connect();
    filter_data($input, $conn);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理