【问题标题】:Problem with migrating PHP5 functions to PHP7将 PHP5 函数迁移到 PHP7 的问题
【发布时间】:2021-01-28 17:50:21
【问题描述】:

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

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

<?php

$db_host='localhost';
$db_user='root';
$db_pass='';
$db_name='migration';

$link = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (mysqli_connect_errno()) {
    echo mysqli_connect_error();
    exit();
}else{
    mysqli_query($link, "SET NAMES utf8");
    mysqli_query($link, "SET CHARACTER SET utf8" );
}

function dbquery($query) {
    $result = mysqli_query($link, $query);
    if (!$result) {
        echo mysqli_error($link);
        return false;
    } else {
        return $result;
    }
}

function dbarray($query) {
    $result = mysqli_fetch_assoc($query);
    if (!$result) {
        echo mysqli_error($link);
        return false;
    } else {
        return $result;
    }
}

// Fetch the Site Settings from the database and store them in the $settings variable
$settings = dbarray(dbquery("SELECT * FROM site_settings"));

?>

现在的问题是,当我运行文件时,出现以下错误:

Warning: mysqli_query() expects parameter 1 to be mysqli, null given
Warning: mysqli_error() expects parameter 1 to be mysqli, null given
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, bool given
Warning: mysqli_error() expects parameter 1 to be mysqli, null given

现在,我在这里做错了什么?

添加

当我启动这个文件时,我在一个单独的函数中有mysqli_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_connect 似乎也没有在函数内部工作。 后来,我把代码改成了我在上面写的样子。

非常感谢这里的帮助。

编辑

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

【问题讨论】:

  • 一定要用mysqli吗?为什么不能切换到 PDO?
  • @Dharman 我一直在使用 PHP5 结构,我不是专业程序员。所以当我决定迁移到 PHP7 时,我认为mysqli 会更容易实现和使用。我不知道 PDO 是如何工作的。
  • 我推荐学习 PDO。它从 PHP 5 开始就可用,它更易于使用并提供更多功能。在这里开始学习phpdelusions.net/pdo
  • @Dharman 非常感谢。我一定会看看 PDO 并尝试学习它。但只是为了我自己的信息,你能告诉我我用mysqli 尝试的代码有什么问题吗?我阅读了一些基本指南,所有内容都是根据这些指南编写的。我只是把它们放在我自己的函数中。

标签: php mysqli


【解决方案1】:

如果你必须使用 mysqli,那么最好有包装函数。但是,你现在拥有的东西是没有用的。你的函数是脱节的,你没有使用参数化的预处理语句并且你没有启用 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;
    }
}

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

【讨论】:

    猜你喜欢
    • 2017-08-04
    • 2016-09-24
    • 2021-01-03
    • 1970-01-01
    • 2020-08-07
    • 2021-05-15
    • 1970-01-01
    • 2018-02-27
    • 2019-03-09
    相关资源
    最近更新 更多