Page 1 of 1

PHP5 database code issue

Posted: Wed Jun 06, 2007 2:09 pm
by christus
I'm new to the whole OOP scene, and I was trying to write some database code. Unfortunately, the following code refuses to display any data, just two breaks (the number of the records in that table). When I do a print_r() on $results, it displays all of the data returned by the query, but as soon as I do the simple echo, it shows nothing at all!

from mssql.class.php:

Code: Select all

//query the database
	      function query($query) {
			try {
				$this->result = @mssql_query($query, $this->linkId);
				if (! $this->result)
					throw new Exception("The query failed. Check your syntax.");
			}
			catch (Exception $e) {
				die($e->getMessage());
			}
			return $this->result;
		}
	
	//put the results into a usable array
		function fetchArray() {
			$array = @mssql_fetch_assoc($this->result);
			return $array;
		}
From the actual script (database.php):

Code: Select all

//run the query	
	$sqlDb->query("SELECT * NAMES");

	while ($results = $sqlDb->fetchArray()) {
		//print_r($results);
		echo ("$results->NAME <br />");
	}
I would sincerely appreciate any help you could offer.

Posted: Wed Jun 06, 2007 4:11 pm
by superdezign
You are treating $results like an object, but it is an array.

Posted: Thu Jun 07, 2007 7:34 am
by christus
Oh wow, thanks. That worked like a charm. I simply changed it to this:

Code: Select all

echo ("$results[NAME] <br />");
if I wanted to return an object, though, would I have to write in the class file like this?

Code: Select all

function fetchArray() {
		$array = @mssql_fetch_assoc($this->result);
		return $this->array;
	}
Or would I have to do something else?

Posted: Thu Jun 07, 2007 7:50 am
by volka
You might be interested in http://de2.php.net/pdo