我是php的新手,我正在尝试开发一个类。我连接到sqlsrv,连接正常。当我运行查询时,结果几乎就像在循环中一样重复。我找不到问题,我希望额外的一双眼睛可以提供帮助。任何帮助表示赞赏!
class DB{
private static $_instance = null;
private $_connection,
$_query,
$_error = false,
$_results,
$_count = 0;
private function __construct(){
$mssqlInfo = array( "Database"=>"db");
$mssqlServer = "server";
$this->_connection = sqlsrv_connect($mssqlServer, $mssqlInfo);
if($this->_connection === false ) {
echo '<h2>Unable to connect to database</h2><br/>';
die( print_r(sqlsrv_errors(), true));
}else{
//echo 'Connected';
}
}
public static function getInstance(){
if(!isset(self::$_instance)){
self::$_instance = new DB();
}
return self::$_instance;
}
public function query($sql, $params = array()){
$this->_error = false;
$options = array( "Scrollable" => SQLSRV_CURSOR_STATIC );
$this->_query = sqlsrv_query($this->_connection, $sql, $params,$options);
if($this->_query){
$this->_results = sqlsrv_fetch_object($this->_query);
$this->_count = sqlsrv_num_rows($this->_query);
}else{
$this->_error = true;
echo 'Error in statement execution.\n';
die( print_r( sqlsrv_errors(), true));
}
return $this;
}
public function action($action, $table, $where = array()){
if(count($where) === 3){
$operators = array('=', '>', '<', '>=', '<=');
$field = $where[0];
$operator = $where[1];
$value = $where[2];
if(in_array($operator, $operators)){
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} (?)";
if(!$this->query($sql, array($value))->error()){
return $this;
}
}
}
return false;
}
public function get($table, $where){
return $this->action('SELECT *', $table, $where);
}
public function delete($table, $where){
return $this->action('DELETE', $table, $where);
}
public function results(){
return $this->_results;
}
public function first(){
return $this->results()[0];
}
public function error(){
return $this->_error;
}
public function count(){
return $this->_count;
}
}
这是我用来测试我的函数和检索查询的索引页面上的实例。
require_once 'core/init.php';
$users = DB::getInstance()->query("SELECT * FROM users");
$user = DB::getInstance()->get('users', array('username', '=', 'mike'));
if(!$user->count()){
echo 'No user';
}else{
$obj = $users->results();
foreach($obj as $users){
echo $users->username;
echo '<br/>';
}
}
我忘了把桌子包括在内。
CREATE TABLE [dbo].[users](
[id] [int] IDENTITY(1,1) NOT NULL,
[username] [varchar](20) NOT NULL,
[password] [varchar](64) NOT NULL,
[salt] [varchar](32) NOT NULL,
[name] [varchar](50) NOT NULL,
[joined] [datetime] NOT NULL,
[group] [int] NOT NULL,
CONSTRAINT [PK_users] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
答案 0 :(得分:0)
看起来你在你的while循环中将$ obj设置为$ user-&gt; results(),所以它总是正确的,因此永远不会停止。
只需将其更改为foreach()。
$obj = $user->results();
foreach ($obj as $user) {
echo $user->username;
echo '<br/>';
}
答案 1 :(得分:0)
在函数query()中只获取一行结果并将其保存到成员_results: $ this-&gt; _results = sqlsrv_fetch_object($ this-&gt; _query);
然后在while循环中使用函数results()。 results()返回保存在member _results中的对象,该对象永远不会更改。这就是你无限循环的原因。
解决您的问题: 变化
public function query($sql, $params = array()){
[...]
$this->_query = sqlsrv_query($this->_connection, $sql, $params,$options);
if($this->_query){
$this->_results = sqlsrv_fetch_object($this->_query);
$this->_count = sqlsrv_num_rows($this->_query);
[...]
到
public function query($sql, $params = array()){
[...]
$this->_query = $sql;
$this->_results = sqlsrv_query($this->_connection, $sql, $params,$options);
if($this->_query){
$this->_count = sqlsrv_num_rows($this->_query);
[...]
并改变
public function results(){
return $this->_results;
}
到
public function results(){
return sqlsrv_fetch_object($this->_results);
}