我们应该在哪里使用fetch_assoc方法? [英] Where should we use fetch_assoc method?

查看:48
本文介绍了我们应该在哪里使用fetch_assoc方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始了这个小博客项目,我认为我没有加入 fetch_assoc 方法就犯了一个错误.这是代码:

I started this little blog project and I think I have just made a mistake by not including fetch_assoc method. This is the code:

$data = new database();
$sql = "SELECT * FROM post ";
 $article = $data->select($sql);
 foreach ($article as  $value) : ?>
 <div class="blog-post">
<h2 class="blog-post-title"><?= $value["blog_title"]; ?></h2>

这是select方法的外观:

and this is how the select methode look like:

public function select($sql){
     $result = $this->con->query($sql) or die($this->con->error.__LINE__);
     if ($result->num_rows > 0) {
        return $result;
     }else {
       return false;
     }
}

我想知道为什么它对我有用(博客标题正确显示),尽管我忘记了使用 fetch_assoc 方法?

I want to know why it has worked for me (the title of the blog shows up correctly) although I have forgotten to put the fetch_assoc method?

推荐答案

您通常不需要使用 fetch_assoc().如果您正在围绕mysqli编写某种抽象类,则您永远不需要直接使用任何mysqli函数.

You do not need to use fetch_assoc() most of the time. If you are writing some kind of abstraction class around mysqli then you should never need to use any of mysqli functions directly.

mysqli:query()返回可迭代的 mysqli_result 类型的对象.这意味着您可以使用 foreach 循环对其进行迭代.

mysqli:query() returns an object of type mysqli_result which is iterable. This means you can use foreach loop to iterate over it.

我需要指出,您仍然遵循一些不良的编程习惯.您永远不要使用或die($ this-> con-> error .__ LINE __).这是一种糟糕的编码实践,完全没有必要.相反,您应该启用正确的错误报告.请参见如何在MySQLi中获取错误消息?

I need to point out that you are still following some bad programming practices. You should never use or die($this->con->error.__LINE__). This is a terrible coding practice and completely unnecessary. Instead, you should enable proper error reporting. See How to get the error message in MySQLi?

您的 select()方法应仅返回一个数组.这将使您的代码更加容易,并且不易出错.因此,将您的 select()函数重写为:

Your select() method should return only an array. This would make your code much easier and less error-prone. So rewrite your select() function to this:

public function select($sql){
    return $this->con->query($sql)->fetch_all(MYSQLI_ASSOC);
}

如您所见,整个功能是一行代码,这意味着您的 select()方法不是很有用.您可以将其替换为更通用的名称.这样可以避免大量的代码重复.

As you can see the whole functionality is a single line of code, which means your select() method is not very useful. You can replace it with something more generic instead. This way you avoid a lot of code repetition.

public function executeQuery(string $sql, array $params = []): ?array {
    // Prepare/bind/execute
    $stmt = $this->con->prepare($sql);
    if ($params) {
        $stmt->bind_param(str_repeat("s", count($params)), ...$params);
    }
    $stmt->execute();
    // If it was a select query which gives result then return results in an array
    if ($result = $stmt->get_result()) {
        return $result->fetch_all(MYSQLI_BOTH);
    }
    // If it is INSERT/UPDATE then return null instead of array
    return null;
}

当然,此方法不应成为模型的一部分.它应该是数据库通信专用类的一部分.然后,您可以在应用程序中创建它的单个实例,并将其传递给模型.

Of course this method should not be part of your model. It should be part of a dedicated class for database communication. You can then create a single instance of it in your application and pass it to your model.

这篇关于我们应该在哪里使用fetch_assoc方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆