mysql_fetch_array() and Classes?
Posted: Tue Mar 06, 2007 3:45 pm
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: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))Now when I doIt 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.
I'm used to using this:
Code: Select all
<?php
while($r = mysql_fetch_array($result)) {
echo "Hello $r[username]";
}
?>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();
}
}
?>Code: Select all
<?php
$test = new DB;
$test->connect();
?>