Php 使用PDO时提取SELECT查询的结果
在PHP中使用PDO时,我很难理解如何提取SELECT查询的结果。这是我的数据库类:Php 使用PDO时提取SELECT查询的结果,php,Php,在PHP中使用PDO时,我很难理解如何提取SELECT查询的结果。这是我的数据库类: class DB { # ATTRIBUTES private static $_instance = null; # stores instance of the db, if it's available private $_pdo, $_query, $_error = false, $_results,
class DB
{
# ATTRIBUTES
private static $_instance = null; # stores instance of the db, if it's available
private $_pdo,
$_query,
$_error = false,
$_results,
$_count = 0;
# METHODS
# connect to database
private function __construct()
{
try
{
$this->_pdo = new PDO('sqlsrv:Server=' . DB_HOST . ';Database=' . DB_NAME);
// echo 'connected'; //enable this line to test db connection
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
# get database instance if it exists, otherwise create a database instance (this prevents repeatedly reconnecting to db)
public static function getInstance()
{
if (!isset(self::$_instance)):
self::$_instance = new DB();
endif;
return self::$_instance;
}
# generic query method
public function query($sql, $parameters = array())
{
$this->_error = false;
if ($this->_query = $this->_pdo->prepare($sql)):
$x = 1;
if (count($parameters)):
foreach($parameters as $parameter):
$this->_query->bindValue($x, $parameter);
$x++;
endforeach;
endif;
if ($this->_query->execute()):
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
else:
$this->_error = true;
endif;
endif;
return $this;
}
# methods to return query results
public function results()
{
return $this->_results;
}
public function firstresult()
{
return $this->results()[0];
}
# method to return errors
public function error()
{
return $this->_error;
}
# method to return query row count
public function count()
{
return $this->_count;
}
}
这是我的测试课:
class XYZ
{
# ATTRIBUTES
private $_db,
$_data;
# METHODS
public function __construct($test = null)
{
$this->_db = DB::getInstance();
}
public function test1()
{
$data = $this->_db->query('SELECT * FROM Users WHERE UserName LIKE ?;', ['Ad%']);
$this->_data = $data;
return $this->_data;
}
}
下面是使用类XYZ创建对象并查看结果:
$x = new XYZ();
$y = $x->test1();
echo '<pre>' . print_r($y,1) . '</pre><br><br>';
为了在类XYZ中访问DB对象中的多维数组(请参见数组键[\u results:DB:private]),需要做什么?问题在于如何编写test1()函数。下面是重新编写的代码:
Array
(
[0] => stdClass Object
(
[UserID] => 1
[UserName] => Admin
[Password] => ²â€ÃœÂµÃ·Â©Ã¶Ã©rºó°TÈÃr^‡
[Email] => admin@website.com
[FirstName] =>
[LastName] =>
[BusinessName] =>
[Registered] => 2009-01-01 00:00:00.0000000
[UserType] => A
[Inserted] => 2014-03-06 19:40:23.6500000
)
[1] => stdClass Object
(
[UserID] => 4
[UserName] => Adam1978
[Password] => ²â€ÃœÂµÃ·Â©Ã¶Ã©rºó°TÈÃr^‡
[Email] => adamjfinley@gmail.com
[FirstName] => Adam
[LastName] => Finley
[BusinessName] =>
[Registered] => 2014-02-14 16:19:22.0000000
[UserType] => R
[Inserted] => 2014-03-06 19:40:23.6500000
)
)
test1()中的$data似乎是DB对象。为了查看SELECT查询的结果,在使用DB对象时可以访问DB类的results()方法(本例中为data)
结果是stdClass对象的多维数组:
public function test1()
{
$data = $this->_db->query('SELECT * FROM Users WHERE UserName LIKE ?;', ['Ad%']);
$this->_data = $data->results();
return $this->_data;
}
Array
(
[0] => stdClass Object
(
[UserID] => 1
[UserName] => Admin
[Password] => ²â€ÃœÂµÃ·Â©Ã¶Ã©rºó°TÈÃr^‡
[Email] => admin@website.com
[FirstName] =>
[LastName] =>
[BusinessName] =>
[Registered] => 2009-01-01 00:00:00.0000000
[UserType] => A
[Inserted] => 2014-03-06 19:40:23.6500000
)
[1] => stdClass Object
(
[UserID] => 4
[UserName] => Adam1978
[Password] => ²â€ÃœÂµÃ·Â©Ã¶Ã©rºó°TÈÃr^‡
[Email] => adamjfinley@gmail.com
[FirstName] => Adam
[LastName] => Finley
[BusinessName] =>
[Registered] => 2014-02-14 16:19:22.0000000
[UserType] => R
[Inserted] => 2014-03-06 19:40:23.6500000
)
)