【问题标题】:Prepared Statements in a PHP database connection classPHP 数据库连接类中的预处理语句
【发布时间】:2016-06-10 10:18:59
【问题描述】:

我了解准备好的语句是如何工作的(在函数之外)。但真正让我感到困惑的是准备好的语句如何在类文件的函数中工作。

例如,如果一个prepared statement的用法是:

$dbc->select('SELECT * FROM users WHERE firstname = ?');

但是,我不知道我对用户如何使用 $dbc->select(); 感到困惑。类文件中的函数,创建一个查询,然后使用准备好的语句来执行查询。

会不会像让用户编写查询然后将变量作为另一个参数一样简单,例如:

$dbc->select('SELECT * FROM users WHERE firstname = ?', 'Bob');

或者我会将它作为单独的变量供用户填写,然后将其构建到准备好的语句中,例如:

$dbc->select($columns, $table, $variables = array());

或者这会使事情变得过于复杂?我还可以有另一个选择特定行的函数。

我觉得选项 1 是更好的选择,但它似乎不那么用户友好?还是我只是太累了,以至于我想得太多了?

无论如何,我希望这一切都是有意义的,对于任何错误我深表歉意 - 我在打字的时候差点睡着了!也许是时候结束了。

谢谢, 基隆

【问题讨论】:

  • 您的目标是使其更高级,例如查询构建器还是只是标准语句执行和返回的包装器?
  • 好吧,我刚起床,我可以解释得更好(因为我不再累了)。我想做的是创建一个数据库类,它将具有常见的 mysqli 函数,如选择、插入、删除、更新等。所以他们会使用我自己制作的函数来查询数据库。我遇到的问题是,当用户进行查询时,我对如何使用准备好的语句感到困惑(我怎么知道哪些列、表等)我在帖子中给出了一些关于我想到的方法的示例,但我不确定它们是否是最佳选择。
  • 没有人能解释在 $dbc->select("SELECT * FROM users WHERE id = "); 这样的函数中应该如何使用准备好的语句?
  • 你所有的例子在课堂上都是可行的。这一切都可以发挥作用。你的第二个可能是最好的想法。第二个参数是可选的。
  • 此外,您还想输入一个数组,而不仅仅是一个值,除非您创建一个 if 条件来处理它,我猜这很容易。

标签: php mysql oop mysqli prepared-statement


【解决方案1】:

正如我所提到的,所有这些都是可行的:

实例一:

class qEngine 
    {
        private $con,
                $bind,
                $query;

        public function __construct($con)
            {
                $this->con = $con;
            }
        public function addParams($array)
            {
                $i = 0;
                foreach($array as $key => $value) {
                    $bKey = ":{$i}";
                    $this->bind[$bKey] = $value;
                    $i++;
                }
                return $this;
            }
        public function query($sql)
            {
                if(!empty($this->bind)) {
                    $this->query = $this->con->prepare($sql);
                    $this->query->execute($this->bind);
                }
                else {
                    $this->query = $this->con->query($sql);
                }
                return $this;
            }
        public function getResults()
            {
                while($result = $this->query->fetch(PDO::FETCH_ASSOC)) {
                    $row[] = $result;
                }
                return (!empty($row))? $row : 0;
            }
    }

你的例子:

$dbc->select('SELECT * FROM users WHERE firstname = ?');

应该是这样的:

// $con would be a PDO connection
$qEngine = new qEngine($con);
$results = $qEngine     ->addParams(array('Bob'))
                        ->query('select * from users where firstname = :0')
                        ->getResults();
print_r($results);

实例二:

class qEngine 
    {
        private $con,
                $bind,
                $query;

        public function __construct($con)
            {
                $this->con = $con;
            }
        private function addParams($array = false)
            {
                if(empty($array))
                    return false;

                $i = 0;
                foreach($array as $key => $value) {
                    $bKey = ":{$i}";
                    $this->bind[$bKey] = $value;
                    $i++;
                }
            }
        public function query($sql,$bind = false)
            {
                $this->addParams($bind);

                if(!empty($this->bind)) {
                    $this->query = $this->con->prepare($sql);
                    $this->query->execute($this->bind);
                }
                else {
                    $this->query = $this->con->query($sql);
                }
                return $this;
            }
        public function getResults()
            {
                while($result = $this->query->fetch(PDO::FETCH_ASSOC)) {
                    $row[] = $result;
                }
                return (!empty($row))? $row : 0;
            }
    }

你的例子:

$dbc->select("SELECT * FROM users WHERE firstname = ?",'Bob');

应该是这样的:

// $con would be a PDO connection
$qEngine    =   new qEngine($con);
$results    =   $qEngine    ->query('select * from users where firstname = :0',array('Bob'))
                            ->getResults();

print_r($results);

实例 3 必须更复杂,因为您必须自动构建 sql。我有一个类似的,但它很复杂。

无论如何,这就是我的大致做法。请注意,我没有广泛使用这些,但如果您在构造中注入了适当的 pdo 连接,它们应该可以工作。

【讨论】:

  • 啊,完美!这让事情变得更加清晰了。所以我可以为常见的sql函数执行不同的特定函数。谢谢!
猜你喜欢
  • 2017-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多