mysql_fetch_array() and 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
Dale
Forum Contributor
Posts: 466
Joined: Fri Jun 21, 2002 5:57 pm
Location: Atherstone, Warks

mysql_fetch_array() and Classes?

Post by Dale »

I've started (after years) deciding to learn Classes within PHP. However i'm stumped. I can connect to the database and also select something from a database (well I think I have as it didn't come up with an error) however i'm unsure with a class how to show the data from the mysql_fetch_array() command.

I'm used to using this:

Code: Select all

<?php
while($r = mysql_fetch_array($result)) {
echo "Hello $r[username]";
}
?>
But trying to do it with a Class doesn't seem to work, PLUS I don't know how to show it.

Here is what I have so far (don't slate me - i'm such a n00b with Classes (I still dont understand Arrays properly either :p))

Code: Select all

<?php
class DB {
	var $db_host = "localhost";
	var $db_user = "username";
	var $db_pass = "password";
	var $db_data = "database";

	function connect() {
		mysql_connect($this->db_host,$this->db_user,$this->db_pass);
		mysql_select_db($this->db_data) or die($this->killed(mysql_error()));
	}

	function quel($thequery) {
		$thequery = mysql_query($thequery) or die($this->killed(mysql_error()));
	}

	function qfet($fetchquery) {
		mysql_fetch_array($this->quel) or die($this->killed(mysql_error()));
		return $this->qfet;
	}

	function killed($whydie) {
		echo "<html><head><title>An Error</title></head><body><font face=\"Verdana\" size=\"2\"><hr><font color=\"#FF0000\"><b>!! ERROR !!</b></font><br>$whydie<hr></font></body></html>";
		die();
	}

	}
?>
Now when I do

Code: Select all

<?php
	$test = new DB;
	$test->connect();
?>
It doesn't give me an error so obviously that works and when I do a query it doesnt give me an error either, so that must mean it's working, yeah? So how would I work it for a mysql_fetch_array() tag?? (I've checked some tutorials also the source code on many Open Source scripts but I still don't understand it.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Both quel() and qfet() will not work as expected.

If you want to go more into full OOP, a query would return a result set object, which would have the fetch methods in it. If you're wanting to simply mash them into a single class, the result set will need to be stored in the class for optimal use.

Currently, the result from mysql_fetch_array() is not being returned and the result from mysql_query() isn't being returned either.

On a side note, you have an odd naming convention.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You might want to start with an existing database class before writing your own. If you're using php5 take a look at http://de2.php.net/pdo
Post Reply