将PHP5函数迁移到PHP7时出现问题

将PHP5函数迁移到PHP7时出现问题,php,mysqli,Php,Mysqli,我的项目中有一个核心文件,其中包含我使用的所有PHP函数。都是用PHP5写的。 最近我决定学习并迁移到PHP7 这是我的主要文件内容(简化): 现在,我做错了什么 添加 当我启动这个文件时,我在一个单独的函数中使用了mysqli\u connect。 所以连接实际上是这样的: $link = dbconnect($db_host, $db_user, $db_pass, $db_name); function dbconnect($db_host, $db_user, $db_pass, $d

我的项目中有一个核心文件,其中包含我使用的所有PHP函数。都是用PHP5写的。 最近我决定学习并迁移到PHP7

这是我的主要文件内容(简化):

现在,我做错了什么

添加

当我启动这个文件时,我在一个单独的函数中使用了
mysqli\u connect
。 所以连接实际上是这样的:

$link = dbconnect($db_host, $db_user, $db_pass, $db_name);

function dbconnect($db_host, $db_user, $db_pass, $db_name) {
    $db_connect = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
    if (mysqli_connect_errno()) {
        echo mysqli_connect_error();
        exit();
    }
    mysqli_query($db_connect, "SET NAMES utf8");
    mysqli_query($db_connect, "SET CHARACTER SET utf8" );
}
但是
mysqli\u connect
在函数内部似乎也不起作用。 后来,我把代码改成我在上面写的代码

我真的很感谢你的帮助

编辑


我还尝试将
$link
作为全局变量。但是这没有帮助。

如果必须使用mysqli,那么最好使用包装函数。然而,你现在拥有的是无用的。您的函数是脱节的,您没有使用参数化的prepared语句,并且您没有启用mysqli异常

您得到的错误是因为您没有在所处的范围内定义变量。每次在方法/函数中需要变量时,都必须将其作为参数传递给函数

我建议改为创建一个类。这个类应该至少有一个helper函数,可以让您快速执行准备好的语句

class DBClass extends mysqli {
    public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
        $this->set_charset('utf8mb4');
    }

    /**
     * Executed prepared statement
     *
     * @param string $sql SQL query with placeholders e.g. SELECT * FROM users WHERE Id=?
     * @param array $params An array of parameters to be bound
     * @return array|null
     */
    public function safeQuery(string $sql, array $params = []): ?array {
        // prepare/bind/execute
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        // If the query produces results then fetch them into multidimensional array
        if ($result = $stmt->get_result()) {
            return $result->fetch_all(MYSQLI_BOTH);
        }
        // return nothing if the query was successfully executed and it didn't produce results
        return null;
    }

    /**
     * Executed prepared statement but fetch a single row only
     *
     * @param string $sql SQL query with placeholders e.g. SELECT * FROM users WHERE Id=?
     * @param array $params An array of parameters to be bound
     * @return array|null
     */
    public function row(string $sql, array $params = []): ?array {
        // prepare/bind/execute
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        // If the query produces results then fetch them into an array
        if ($result = $stmt->get_result()) {
            // here we use fetch_assoc to get a single associative array
            return $result->fetch_assoc();
        }
        // return nothing if the query was successfully executed and it didn't produce results
        return null;
    }
}

这只是一个演示,但您可以遵循相同的设计。但是,我建议使用现有的解决方案,而不是重新发明轮子。我推荐一个非常好的DB抽象层。

如果必须使用mysqli,那么最好使用包装函数。然而,你现在拥有的是无用的。您的函数是脱节的,您没有使用参数化的prepared语句,并且您没有启用mysqli异常

您得到的错误是因为您没有在所处的范围内定义变量。每次在方法/函数中需要变量时,都必须将其作为参数传递给函数

我建议改为创建一个类。这个类应该至少有一个helper函数,可以让您快速执行准备好的语句

class DBClass extends mysqli {
    public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
        $this->set_charset('utf8mb4');
    }

    /**
     * Executed prepared statement
     *
     * @param string $sql SQL query with placeholders e.g. SELECT * FROM users WHERE Id=?
     * @param array $params An array of parameters to be bound
     * @return array|null
     */
    public function safeQuery(string $sql, array $params = []): ?array {
        // prepare/bind/execute
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        // If the query produces results then fetch them into multidimensional array
        if ($result = $stmt->get_result()) {
            return $result->fetch_all(MYSQLI_BOTH);
        }
        // return nothing if the query was successfully executed and it didn't produce results
        return null;
    }

    /**
     * Executed prepared statement but fetch a single row only
     *
     * @param string $sql SQL query with placeholders e.g. SELECT * FROM users WHERE Id=?
     * @param array $params An array of parameters to be bound
     * @return array|null
     */
    public function row(string $sql, array $params = []): ?array {
        // prepare/bind/execute
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        // If the query produces results then fetch them into an array
        if ($result = $stmt->get_result()) {
            // here we use fetch_assoc to get a single associative array
            return $result->fetch_assoc();
        }
        // return nothing if the query was successfully executed and it didn't produce results
        return null;
    }
}

这只是一个演示,但您可以遵循相同的设计。但是,我建议使用现有的解决方案,而不是重新发明轮子。我推荐一个非常好的数据库抽象层。

你必须使用mysqli吗?为什么你不能切换到PDO?@Dharman我一直在使用PHP5结构,而我不是一个专业的程序员。因此,当我决定迁移到PHP7时,我认为
mysqli
将更易于实现和使用。我不知道PDO是如何工作的。我建议学习PDO。它从PHP5开始就可以使用了,而且它更易于使用,提供了更多的功能。在这里开始学习@Dharman非常感谢。我一定会看一看PDO并尝试学习它。但仅供我参考,你能告诉我我用
mysqli
尝试的代码有什么问题吗?我读了一些基本的指南,所有的东西都是根据这些指南写的。我只是把它们放在我自己的函数中。你必须使用mysqli吗?为什么你不能切换到PDO?@Dharman我一直在使用PHP5结构,而我不是一个专业的程序员。因此,当我决定迁移到PHP7时,我认为
mysqli
将更易于实现和使用。我不知道PDO是如何工作的。我建议学习PDO。它从PHP5开始就可以使用了,而且它更易于使用,提供了更多的功能。在这里开始学习@Dharman非常感谢。我一定会看一看PDO并尝试学习它。但仅供我参考,你能告诉我我用
mysqli
尝试的代码有什么问题吗?我读了一些基本的指南,所有的东西都是根据这些指南写的。我只是把它们放在我自己的函数中。
class DBClass extends mysqli {
    public function __construct($host = null, $username = null, $passwd = null, $dbname = null, $port = null, $socket = null) {
        mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
        parent::__construct($host, $username, $passwd, $dbname, $port, $socket);
        $this->set_charset('utf8mb4');
    }

    /**
     * Executed prepared statement
     *
     * @param string $sql SQL query with placeholders e.g. SELECT * FROM users WHERE Id=?
     * @param array $params An array of parameters to be bound
     * @return array|null
     */
    public function safeQuery(string $sql, array $params = []): ?array {
        // prepare/bind/execute
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        // If the query produces results then fetch them into multidimensional array
        if ($result = $stmt->get_result()) {
            return $result->fetch_all(MYSQLI_BOTH);
        }
        // return nothing if the query was successfully executed and it didn't produce results
        return null;
    }

    /**
     * Executed prepared statement but fetch a single row only
     *
     * @param string $sql SQL query with placeholders e.g. SELECT * FROM users WHERE Id=?
     * @param array $params An array of parameters to be bound
     * @return array|null
     */
    public function row(string $sql, array $params = []): ?array {
        // prepare/bind/execute
        $stmt = $this->prepare($sql);
        if ($params) {
            $stmt->bind_param(str_repeat("s", count($params)), ...$params);
        }
        $stmt->execute();
        // If the query produces results then fetch them into an array
        if ($result = $stmt->get_result()) {
            // here we use fetch_assoc to get a single associative array
            return $result->fetch_assoc();
        }
        // return nothing if the query was successfully executed and it didn't produce results
        return null;
    }
}