The following is a class object I'm in the process of knocking up. For the record, I am using PHP 4.0.1pl2 compiled as a static thingy in Apache 1.3.12. The whole lot has been compiled with gcc 2.7.2.3 and is running under SCO Openserver 5.0.5. I am attempting to talk to MS SQL Server 7 with FreeTDS 0.51 doing the do. My shoe size is 11 (British).
This ain't working. When my PHP script starts up, it calls the object's constructor which initialises the connection with mssql_connect and stores the connection handle in $dbh. I then call object->dbQuery("select from MyTable"). This stores a handle to the result set in $rset. This works, because I can dump out the contents of the table with while($row = mssql_fetch_row($this->rset)) { echo "$row[0]"; }, which also works. But* if I then call object->dbField(0) to return, say, an object representing the first field, I get:
Warning: Supplied argument is not a valid Sybase result resource in class/dbConnect.php on line 30
line 30 being the mssql_num_fields line below. Am I showing my profound ignorance of PHP here by suggesting that $this->rset is only in scope in object->dbQuery(), which would be silly. Help!
PS. There seems to be the odd inconsistency between the documentation and the mssql_
<?php
class dbConnect
{
var $dbh; // Database connection handle
var $rset; // Result set from the last call to dbQuery
function dbConnect($host, $user, $auth)
// Attempts to initiate a connection to the database listed in the Sybase
// interfaces file as $host, with $user and $auth as the authentication
// details
{
$this->dbh = mssql_connect($host, $user, $auth)
or die("ERROR: failed to connect to MS SQL Server");
}
function dbQuery($sql)
// Runs the specified SQL query on the database
{
$this->rset = mssql_query($sql, $this->dbh)
or die("ERROR: cannot run $sql");
}
function dbField($fieldno)
// Returns the name of the fieldno-th field in the
{
if ($fieldno >= mssql_num_fields($this->rset)) // line 30
{
return;
}
else
{
return(mssql_fetch_field($this->rset, $fieldno));
}
}
}
?>