Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何避免mysqli连接以及如何使用real\u escape\u字符串?_Php_Mysql_Oop - Fatal编程技术网

Php 如何避免mysqli连接以及如何使用real\u escape\u字符串?

Php 如何避免mysqli连接以及如何使用real\u escape\u字符串?,php,mysql,oop,Php,Mysql,Oop,下面是php类的摘录: DB.class.php insert entry.php 关于本课程的专业使用,我有三个问题: 避免mysqli连接:该类以前是mysql,我将其更改为mysqli。当我想要执行mysqli操作(例如mysqli_query())时,是否有任何方法可以避免我总是需要将mysql连接作为参数传递 清理用户输入:如何生成$db->real\u escape\u字符串?我希望它应用于来自用户并进入我的数据库的所有值。它是我可以添加到DB.class.php中的insert函

下面是php类的摘录:

DB.class.php

insert entry.php

关于本课程的专业使用,我有三个问题:

  • 避免mysqli连接:该类以前是mysql,我将其更改为mysqli。当我想要执行mysqli操作(例如mysqli_query())时,是否有任何方法可以避免我总是需要将mysql连接作为参数传递
  • 清理用户输入:如何生成$db->real\u escape\u字符串?我希望它应用于来自用户并进入我的数据库的所有值。它是我可以添加到DB.class.php中的insert函数中的东西,还是我必须在外部php文件中应用它,例如本例中的insert-entry.php
  • 捕获错误:在外部php文件(如本例中的insert-entry.php)中,如何 我是否可以捕获函数的错误,例如$db->insert()函数?我的目标是 如果发生错误,用户将被引导到错误页面(通过 标题:位置)。重要提示:我希望能够在不同的php文件中创建单独的错误处理
请在每篇文章中只问一个问题。使用面向对象的方法,而不是通常不需要的过程性方法
real\u escape\u string
-使用参数化方法。您的insert目前易受SQL注入攻击。我建议使用PDO并绑定变量。简单的答案是:使用MVC框架,这项工作已经为您完成。
class DB {    
    protected $db_name = '';
    protected $db_user = '';
    protected $db_pass = '';
    protected $db_host = '';
    protected $connection;

    public function connect() {
        $this->connection = mysqli_connect($this->db_host, $this->db_user, $this->db_pass);
        mysqli_select_db($this->connection, $this->db_name);

        return true;
    }

    public function processRowSet($rowSet, $singleRow = false) {
        $resultArray = array();
        while ($row = mysqli_fetch_assoc($rowSet)) {
            array_push($resultArray, $row);
        }

        if ($singleRow === true)
            return $resultArray[0];

        return $resultArray;
    }

    public function insert($data, $table) {
        $columns = "";
        $values = "";

        foreach ($data as $column => $value) {
            $columns .= ($columns == "") ? "" : ", ";
            $columns .= $column;
            $values .= ($values == "") ? "" : ", ";
            $values .= $value;
        }

        $sql = "insert into $table ($columns) values ($values)";

        mysqli_query($this->connection, $sql) or die(mysqli_error($this->connection));

        //return the ID of the user in the database.
        return mysqli_insert_id($this->connection);
    }
}
require_once 'db.php';

$headline = $_POST['headline'];
$description = $_POST['description'];

$data = array(
        'headline' => $headline,
        'description' => $description
    );

$db->insert($data, 'entries');´