Problem with using php classes

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
ferio-moreno
Forum Commoner
Posts: 30
Joined: Tue Feb 28, 2006 10:31 pm
Location: florida

Problem with using php classes

Post by ferio-moreno »

Hiya folks.

I'm currently building a personal site of my own, and i'm having a bit of a problem as far as getting information from the database using adodb. Its kinda hard for me to explain so I'll print out the code.

Code: Select all

require_once('/adodb/adob.inc.php');
	$db = ADONewConnection('mysql');
	$db->Connect("host","username","pass","database");
	$sql = "SELECT * FROM updates";
	$rs = $db->Execute($sql) or die("SQL Error : ".$db->ErrorMsg());

	while(!$rs->EOF){
		echo $rs->fields('title')."<br/>".$rs->fields('body')."<br><br>";
		$rs->MoveNext();
	}
The above code works just fine. No problems at all, but I was trying to put the connection part into a class so that I wouldn't have to type in those lines all the time, all I would have to do is call to the class object and execute my query from there on.

Code: Select all

class data {
		function data(){
			$db = ADONewConnection('mysql');
			$db->Connect("host","username","pass","database");
		}
	}

	$nd = new data;
	$sql = "SELECT * FROM updates";	
        $rs = $nd->Execute($sql); 

	while(!$rs->EOF){
		echo $rs->fields('title')."<br/>".$rs->fields('body')."<br><br>";
		$rs->MoveNext();
	}
This code doesn't work, I would like it to(thats the way I would like to call the class) but it keeps saying :
Fatal error: Call to undefined function: fields() in /home/www/host_name/classes/class.connect.php on line 28
any suggestions? I'm on PHP4.4xx so the '__construct()' function is kinda useless to me at the moment.
bdlang
Forum Contributor
Posts: 395
Joined: Tue May 16, 2006 8:46 pm
Location: Ventura, CA US

Post by bdlang »

Hmm, well in the adodb PHP documentation, there does appear to be a method named Fields, but none named fields for the ADORecordSet object returned. Edit your code and try again.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

PHP 4:

Code: Select all

class data {

                var $db;

                function data(){
                        $this->db = ADONewConnection('mysql');
                        $this->db->Connect("host","username","pass","database");
                }
        }

    $nd = new data();
    $sql = "SELECT * FROM updates";
    $rs = $nd->db->Execute($sql);
Reason: $nd is an instance of data, not an instance of ADOConnection. Therefore you need to make db publicly acessible and call it via the nd instance
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Jamiel is right. There are better solutions, but only if you're using PHP 5. Call phpinfo() to find out your php version if you're interested
ferio-moreno
Forum Commoner
Posts: 30
Joined: Tue Feb 28, 2006 10:31 pm
Location: florida

Post by ferio-moreno »

yes!!!! thanx alot, it works beautifully. lol. Cant believe I've been getting such a headache over something so simple. :P Thx again :D
Post Reply