PHP 数据库连接 class 中的准备语句
Prepared Statements in a PHP database connection class
我了解准备好的语句是如何工作的(在函数之外)。但真正让我感到困惑的是准备好的语句如何在 class 文件的函数中工作。
例如,如果预处理语句的用法是:
$dbc->select('SELECT * FROM users WHERE firstname = ?');
但是,我不知道自己对用户如何使用 $dbc->select(); 感到困惑。 class 文件中的函数,创建查询,然后使用准备好的语句执行查询。
会不会像让用户编写查询然后将变量作为另一个参数一样简单,例如:
$dbc->select('SELECT * FROM users WHERE firstname = ?', 'Bob');
或者我会把它作为单独的变量供用户填写,然后将其构建到准备好的语句中,例如:
$dbc->select($columns, $table, $variables = array());
或者这会使事情复杂化吗?我还可以有另一个函数 select 是一个特定的行。
我觉得选项 1 是更好的选择,但似乎不太友好?或者我只是太累了以至于我想得太多了 sh**?
无论如何,我希望这一切都是有道理的,对于任何错误我深表歉意 - 我在打字时差点睡着了!也许是时候结束了。
谢谢,
基隆
正如我提到的,所有这些都是可行的:
实例一:
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);
实例三必须更复杂,因为您必须自动构建 sql。我有一个我用的类似的,但它很复杂。
无论如何,这就是我的大致做法。请注意,我没有广泛使用这些,但只要您在构造中注入了适当的 pdo 连接,它们就应该可以工作。
我了解准备好的语句是如何工作的(在函数之外)。但真正让我感到困惑的是准备好的语句如何在 class 文件的函数中工作。
例如,如果预处理语句的用法是:
$dbc->select('SELECT * FROM users WHERE firstname = ?');
但是,我不知道自己对用户如何使用 $dbc->select(); 感到困惑。 class 文件中的函数,创建查询,然后使用准备好的语句执行查询。
会不会像让用户编写查询然后将变量作为另一个参数一样简单,例如:
$dbc->select('SELECT * FROM users WHERE firstname = ?', 'Bob');
或者我会把它作为单独的变量供用户填写,然后将其构建到准备好的语句中,例如:
$dbc->select($columns, $table, $variables = array());
或者这会使事情复杂化吗?我还可以有另一个函数 select 是一个特定的行。
我觉得选项 1 是更好的选择,但似乎不太友好?或者我只是太累了以至于我想得太多了 sh**?
无论如何,我希望这一切都是有道理的,对于任何错误我深表歉意 - 我在打字时差点睡着了!也许是时候结束了。
谢谢, 基隆
正如我提到的,所有这些都是可行的:
实例一:
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);
实例三必须更复杂,因为您必须自动构建 sql。我有一个我用的类似的,但它很复杂。
无论如何,这就是我的大致做法。请注意,我没有广泛使用这些,但只要您在构造中注入了适当的 pdo 连接,它们就应该可以工作。