在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,
$_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>';
结果似乎是数据库对象的内容:
DB Object
(
[_pdo:DB:private] => PDO Object
(
)
[_query:DB:private] => PDOStatement Object
(
[queryString] => SELECT * FROM Users WHERE UserName LIKE ?;
)
[_error:DB:private] =>
[_results:DB:private] => 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
)
)
[_count:DB:private] => 2
)
需要做什么才能在类对象中访问数据库对象中的多维数组(参见数组键[_results:DB:private])?
答案 0 :(得分:1)
问题是如何编写test1()函数。这是重写的,代码有效:
public function test1()
{
$data = $this->_db->query('SELECT * FROM Users WHERE UserName LIKE ?;', ['Ad%']);
$this->_data = $data->results();
return $this->_data;
}
test1()中的$ data似乎是数据库对象。为了查看SELECT查询的结果,使用DB对象时可以访问DB类的results()方法(在这种情况下为$ data)。
结果是stdClass对象的多维数组:
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
)
)