mssql数据库类在下一个结果中出错


mssql database class with getting error on next results

我正试图通过用php构建一个mssql类来了解更多关于OOP的信息。

我遇到了以下错误"警告:mssql_next_result()要求参数1为resource,布尔值在中给定"。。。

我知道问题出在哪里…我的executeQuery()方法返回值"true"并且导致错误。

但是该方法是通过使用$this->result=$result来设置变量$result;在课堂上。在我的脑海中,这就是我想要的查询结果所在。但我不知道如何得到它。

有没有更好的方法来构建这个方法来测试连接并返回我需要的值,或者我应该以另一种方式直接连接到变量。

谢谢你的帮助。

但我不知道如何正确/正确地调用它以获得while(mssql_next_result($rs));因为$rs是一个bool,它需要是一个数组。

有趣的是……尽管这是一件很糟糕的事情,但我仍在构建表,所以我的代码正在工作,只是没有出错。

所以我再次知道错误是我只是不知道如何修复它。

我的代码

<?php
// create new instance of mssql Class
$Users = new SQLServer();
// use connect() method
$Users->connect();

// get user list
$rs = array();
$rs = $Users->executeQuery('Select * FROM Users');

    if (!$Users->numRows($rs)){
        echo 'No records found';
    } else {
    do {
            while ($row = $Users->fetchArray($rs)){
                    echo 'DO STUFF';
            }
        }
        while (mssql_next_result($rs)); 
    }

    $Users->freeResult();
    $Users->disconnect();
 ?>

我的班级

<?php 
    class SQLServer { 
        //connection parameters
        private $db; 
        private $host; 
        private $user; 
        private $password; 
        //handle the connection 
        private $conn; 
        //query results
        public $result; 
        public $numReg; 
        /** 
        * Constructor 
        * @param $db database name
        * @param $host name do host database
        * @param $user  database user name
        $ @param $password database password
        */
        static function SQLServer() { 
            $varDb          = "xxxxxxxxxxxxxx"; 
            $varHost        = "xxxxxxxxxxxxxxxx"; 
            $varuser        = "xxxxxxxxxxxxxx"; 
            $varpassword    = "xxxxxxxxxxxxxxxx"; 
            $this->db       = $varDb; 
            $this->host     = $varHost; 
            $this->user     = $varuser; 
            $this->password = $varpassword;  
        } 
        /** 
        * Connect to mssql and returns true if it automatically selects the base 
        */
        function connect() { 
            $this->conn = mssql_connect($this->host, $this->user, $this->password) or die("Database Connection Error"."<br>". mssql_get_last_message()); 
            if ($this->conn) { 
                mssql_select_db($this->db, $this->conn); 
            }     
        } 
        /** 
        * Return a query 
        * @param $str string valid mssql 
        * @return true query execture com Ãxito, false error na query 
        */
        function executeQuery($str) { 
            $result = mssql_query("$str") or die("Error executing Query"."<br>".mssql_get_last_message()); 
            if ($result) { 
                $this->result = $result; 
                $this->numReg = mssql_num_rows($this->result); 
                return  true; 
            } 
        } 

        /** 
        * Return index line of query results
        * @result index a line of query results 
        */
        function fetchRow() { 
            return mssql_fetch_row($this->result); 
        } 
        /** 
        * Returns the contents of one cell from a MSSQL result set.
        * @result indexed rows from a executed query
        * @Param $line an indexed line of executed query
        * @Param $field string t index row from a  executed query
        */
        function result($line, $field) { 
            return mssql_result($this->result, $line, $field); 
        } 
        /** 
        * Returns a result row as an associative array, a numeric array, or both
        * @result array of indexes of names and a row of query results
        */
        function fetchArray() { 
            return mssql_fetch_array($this->result, MSSQL_BOTH ); 
        } 
         /** 
        * Proceeds to next record.
        * Not working
        */
        function nextResult() { 
             return mssql_next_results($this->result); 
        }
        /** 
        * Returns  the number of records affected by the query
        * @result number of rows affected
        */
        public function affectedRows() { 
            return mssql_rows_affected($this->result); 
        } 
        /** 
        * Return Gets the number of rows in result arary
        * @result number of lines
        */
        public function numRows() { 
            return mssql_num_rows($this->result); 
        } 
        /** 
        * Clears the pointer results 
        * @result clean results pointer 
        */
        public function freeResult() { 
            return mssql_free_result($this->result); 
        } 
        /** 
        * Disconnect from the database
        * @result disconnect database 
        */
        public function disconnect() { 
             return mssql_close($this->conn); 
        }

    } 
?>

您对错误的看法是正确的。您正在尝试循环使用true。数据所在的位置不是$rs,而是放在$this->result中。您这样做是错误的,因为$result = true$result变量中放入了一个布尔值。

当前您的$result变量只能包含1个值。我怀疑您想添加多行,所以将其更改为数组:$this->results = array();

然后更改executeQuery函数,将查询的行添加到$results变量":

function executeQuery($str) { 
    $result = mssql_query("$str") or die("Error executing Query"."<br>".mssql_get_last_message()); 
    if ($result) { 
        while ($row = mssql_fetch_array($result)) {
            $this->results[] = $row;
        }
        $this->numReg = mssql_num_rows($this->result); 
        return  true; 
    } 
} 

现在,您已经将这些行存储在$results变量中。因为它是一个公共变量,所以可以使用$users->results访问它。因此,如果你想循环通过数据,你可以使用这样的东西:

foreach($Users->results as $row){
    echo $row['A_COLUMN_NAME_OF_THE_TABLE_USERS'];
}

请注意,我确实用getter和setter而不是公共变量重新推荐了一个私有/受保护的变量。

如果你想进行行循环并在类中获得行字段,你可以这样做:

class SQLServer { 
    //connection parameters
    private $db; 
    private $host; 
    private $user; 
    private $password; 
    //handle the connection 
    private $conn; 
    private $currentRow = -1; 
    //query results
    public $result; 
    public $numReg; 
    public $results = array(); 

    /** 
    * Constructor 
    * @param $db database name
    * @param $host name do host database
    * @param $user  database user name
    $ @param $password database password
    */
    static function SQLServer() { 
        $varDb          = "xxxxxxxxxxxxxx"; 
        $varHost        = "xxxxxxxxxxxxxxxx"; 
        $varuser        = "xxxxxxxxxxxxxx"; 
        $varpassword    = "xxxxxxxxxxxxxxxx"; 
        $this->db       = $varDb; 
        $this->host     = $varHost; 
        $this->user     = $varuser; 
        $this->password = $varpassword;  
    } 
    /** 
    * Connect to mssql and returns true if it automatically selects the base 
    */
    function connect() { 
        $this->conn = mssql_connect($this->host, $this->user, $this->password) or die("Database Connection Error"."<br>". mssql_get_last_message()); 
        if ($this->conn) { 
            mssql_select_db($this->db, $this->conn); 
        }     
    } 
    /** 
    * Return a query 
    * @param $str string valid mssql 
    * @return true query execture com Ãxito, false error na query 
    */
    function executeQuery($str) { 
        $result = mssql_query("$str") or die("Error executing Query"."<br>".mssql_get_last_message()); 
        if ($result) { 
            while ($row = mssql_fetch_array($result)) {
                $this->results[] = $row;
            }           
            $this->result = $result; 
            $this->numReg = mssql_num_rows($this->result); 
            return  true; 
        } 
        return false;
    }       
    //Get a field of the current row
    function getField($field) { 
        return $this->results[$this->currentRow][$field]; 
    }       
    //Checks if there is a next row
    function nextRow() {
         $this->currentRow++;
         return $this->currentRow <= ($this->numReg - 1); 
    }
} 

您可以将其用于例如:

$Users = new SQLServer();
$Users->connect();
$rs = $Users->executeQuery('Select * FROM Users');
if($rs)
{
    while ($Users->nextRow()){
        echo $Users->getField('USER_FIELD_1');
        echo $Users->getField('USER_FIELD_2');
    }
}