普通网友 2016-11-18 23:09
浏览 51
已采纳

结果在DB :: getInstance中重复

I am new to php and I am trying to develop a class. I am connected to sqlsrv and the connection is fine. Its when I run the query the results are repeating almost like it's in a loop. I can not find the problem and I was hoping an extra pair of eyes can help. Any help is appreciated!

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.
'; 
            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;
}


 }

here is the instance on the index page that I am using to test my functions and to retrieve the query.

    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/>';
    }
}

I forgot to included the table.

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
  • 写回答

2条回答 默认 最新

  • droi5225 2016-11-19 15:25
    关注

    You fetch only one row of the results in function query() and save it to member _results: $this->_results = sqlsrv_fetch_object($this->_query);

    Then you use function results() in a while loop. results() returns an object saved in member _results which will never change. That's why you got an endless loop.

    Solution for your problem: Change

    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);
            [...]
    

    to

    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);
            [...]
    

    And change

    public function results(){
        return $this->_results;
    }
    

    to

    public function results(){
        return sqlsrv_fetch_object($this->_results);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分