So I'm guessing this is not even close to being right, if what is it I am trying to do is doable. I am pretty new to php, comparitavely to many here, and I haven't yet written a working class that can be used at all, so I know I am not doing something right,
Im trying to write a database class that will create my connection to my mysql db in the first function, but i also want it to contain functions for all the common pieces of code used when querying the db, fetching the data, or updating, deleting data etc. That way I can just create an instance of the class, in other functions which will perform different tasks with data in the db, so with each new reference to the class, i can use the methods defined in it to do for example:
$db->new database();
$db->open();
$db->query("SELECT blah blah blah");
while (ssomething = $db->get_row() ) {
}
I have a feeling that I need a good amount more to this code but
here's what I have so far for the database class:
class database {
public $dbname = 'dbname'; //name
public $dbuser = 'dbuser'; // user
public $dbpass = 'dbpass'; // pass
public $dbhost = 'dbhost'; // host
public function open() {
// select db
mysql_select_db ($this->dbname) OR die ('Could not select database: ' . mysql_error() );
// connect to db
mysql_connect ($this->dbhost, $this->dbuser, $this->dbpass)
OR die ('Count not connect to MySQL: ' . mysql() );
}
public function query() {
mysql_query();
}
public function num_rows() {
mysql_num_rows($this->query() );
public function get_row() {
mysql_fetch_array($this->query(), MYSQL_NUM);
}
public function close() {
mysql_close();
}
}
And here's an example of how I'm trying to use it in a separate function that would get a list of categories for urls in my db:
require ('class.db.php');
function show_categories() {
$db = new database(); // this should create a new instance so i think
$db->open(); // opens the connection
$db->query("SELECT * FROM link_cat ORDER BY category DESC");
$output = ''; // self explainatory i think
while ($category = $db->get_row() ) { // will fetch_array, or num_rows
$cat_id = $category['link_cat'];
$name = $category['category'];
$output .= '<li><a href="category.php?cat=' . $cat . '">' . $name . '</a></li>\n';
}
$db->close(); // closes the connection
return $output;
}
So if anyone can help me figure out what I didn't do right, or just need to add, or if this is not possible this way exactly then please let me know how I would need to go about doing it otherwise,
Thanks alot