PHP5 database code issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
christus
Forum Newbie
Posts: 5
Joined: Wed Jun 06, 2007 2:03 pm

PHP5 database code issue

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

You are treating $results like an object, but it is an array.
christus
Forum Newbie
Posts: 5
Joined: Wed Jun 06, 2007 2:03 pm

Post 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?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You might be interested in http://de2.php.net/pdo
Post Reply