如何在类中使用sql查询

时间:2022-06-28 15:09:32

I have a question on what is the best way of implementing SQL queries in PHP classes. I want to keep the queries as low as possible.

我有一个问题,在PHP类中实现SQL查询的最佳方法是什么。我想尽可能低地保持查询。

This was my first attempt:

这是我的第一次尝试:

class NewsArticle
{
    //attributes
    private $newsArticleID;

    //methodes
    //constructoren
    public function __construct($newsArticleID)
    {
        $this->newsArticleID = $newsArticleID;
    }

    //getters   
    public function getGeneralData()
    {
        $query  = mysql_query("SELECT author, title, content, datetime, tags FROM news WHERE news_id = '$this->newsArticleID'");
        $result = mysql_fetch_array($query);

        $data = array(
            'author' => $result['author'],
            'title' => $result['title'],
            'content' => $result['content'],
            'datetime' => $result['datetime'],
            'tags' => $result['tags']
            );

        return $data;
    }

    //setters
}

Now I'm wondering if it would be better to create a setter for generalData and for each item I retrieve from the database create a variable and assign the proper value. Then I could create a getter for each variable. So like this.

现在我想知道为generalData创建一个setter是否更好,并且我从数据库中检索的每个项目都创建一个变量并分配正确的值。然后我可以为每个变量创建一个getter。就像这样。

class NewsArticle
{
    //attributen
    private $newsArticleID;
    private $author;
    private $title;
    // more variables

    //methodes
    //constructoren
    public function __construct($newsArticleID)
    {
        $this->newsArticleID = $newsArticleID;
    }

    //getters    
    public function getAuthor()
    {
        return $this->author;
    }

    public function getTitle()
    {
        return $this->title;
    }

    //setters
    public function setGeneralData()
    {
        $query  = mysql_query("SELECT author, title, content, datetime, tags FROM news WHERE news_id = '$this->newsArticleID'");
        $result = mysql_fetch_array($query);

        $this->author = $result['author'];
        $this->author = $result['author'];

        //other variables
    }
}

2 个解决方案

#1


0  

The point of using setters and getters is that by forcing the developer to use these you can implement more complex access mechanisms, for example setting a flag when a value is set so that your class knows to write it back into the database (although this is a rather bad example as the objective can be acheived without having to force access via a method).

使用setter和getter的关键在于,通过强制开发人员使用它们,您可以实现更复杂的访问机制,例如在设置值时设置标志,以便您的类知道将其写回数据库(尽管这是一个相当糟糕的例子,因为无需通过方法强制访问就可以实现目标。

BTW

news_id = '$this->newsArticleID'

No.

Numbers shouldn't be quoted (breaks the optimizer) and strings should be escaped at the point where they are used in a query.

不应引用数字(打破优化程序),并且应在查询中使用它们时转义字符串。

#2


0  

My advice for you would be to start using PHP-PDO and stored procedures of MySQL. Although you will see a small time difference in using direct queries in php code and in using stored procedures (a difference of milliseconds), you will have less code to write in php. To my opinion that way you would have a more BALANCED application. Also use getData and setData functions that should return anything you want in a form of array or json (i think you will find this very interesting in case you want to use a browser's local storage).

我对你的建议是开始使用MySQL的PHP​​-PDO和存储过程。虽然在PHP代码中使用直接查询和使用存储过程(毫秒差异)时会看到一个小的时间差异,但是在php中编写代码的次数会减少。以我的意见,你会有一个更平衡的应用程序。还可以使用getData和setData函数,这些函数应该以数组或json的形式返回任何你想要的东西(我想如果你想使用浏览器的本地存储,你会发现这非常有趣)。

#1


0  

The point of using setters and getters is that by forcing the developer to use these you can implement more complex access mechanisms, for example setting a flag when a value is set so that your class knows to write it back into the database (although this is a rather bad example as the objective can be acheived without having to force access via a method).

使用setter和getter的关键在于,通过强制开发人员使用它们,您可以实现更复杂的访问机制,例如在设置值时设置标志,以便您的类知道将其写回数据库(尽管这是一个相当糟糕的例子,因为无需通过方法强制访问就可以实现目标。

BTW

news_id = '$this->newsArticleID'

No.

Numbers shouldn't be quoted (breaks the optimizer) and strings should be escaped at the point where they are used in a query.

不应引用数字(打破优化程序),并且应在查询中使用它们时转义字符串。

#2


0  

My advice for you would be to start using PHP-PDO and stored procedures of MySQL. Although you will see a small time difference in using direct queries in php code and in using stored procedures (a difference of milliseconds), you will have less code to write in php. To my opinion that way you would have a more BALANCED application. Also use getData and setData functions that should return anything you want in a form of array or json (i think you will find this very interesting in case you want to use a browser's local storage).

我对你的建议是开始使用MySQL的PHP​​-PDO和存储过程。虽然在PHP代码中使用直接查询和使用存储过程(毫秒差异)时会看到一个小的时间差异,但是在php中编写代码的次数会减少。以我的意见,你会有一个更平衡的应用程序。还可以使用getData和setData函数,这些函数应该以数组或json的形式返回任何你想要的东西(我想如果你想使用浏览器的本地存储,你会发现这非常有趣)。