Page 1 of 1

Problem with using php classes

Posted: Sat Jul 08, 2006 11:39 pm
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.

Posted: Sun Jul 09, 2006 12:25 am
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.

Posted: Sun Jul 09, 2006 4:32 am
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

Posted: Sun Jul 09, 2006 6:49 am
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

Posted: Sun Jul 09, 2006 7:16 pm
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