我正在尝试通过使用php构建一个mssql类来了解有关OOP的更多信息。
我遇到了这个错误"警告:mssql_next_result()期望参数1是资源,在&#34中给出布尔值... ...
我明白问题是什么...我的executeQuery()方法返回" true"的值。 并导致错误。
但该方法是使用$ this-> result = $ result设置变量$ result;在课堂上。在我看来,这就是我想要的查询结果。但我不知道如何得到它。
有没有更好的方法来构建此方法来测试连接并返回我需要的值,或者我应该以另一种方式直接连接到变量。
感谢您的帮助。
但我不确定如何正确/正确地调用它来获取while(mssql_next_result($ rs));工作,因为$ rs是一个bool,它需要是一个数组。
有趣的是......即使它是一个bool,do while仍在构建表,所以我的代码工作它只是没有没有错误。
所以我再次知道错误是我不知道如何修复它。
<?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);
}
}
?>
答案 0 :(得分:0)
你错了。您正试图遍历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');
}
}