Jump to content

My first Class... (Move me to OOP please... sorry)


Aureole

Recommended Posts

EDIT: Crap, wrong Forum... and I forgot to edit out my MYSQL connection details... *goes to change username and password*, please don't hack me kthx...

 

Hey I'm wondering if I'm going about things in the right way so far... I made a db class... here's what I have so far...

 

<?php
class database {

    var $db_host;
    var $db_user;
    var $db_pass;
    var $db_name;
    var $db_connect;
    var $db_disconnect;
    var $db_select;
    var $result;
    var $output;
    var $snr_from;

    function connect() {
$this->db_host = 'localhost';
$this->db_user = '';
$this->db_pass = '';
$this->db_name = '';
$this->db_connect = mysql_connect($this->db_host, $this->db_user, $this->db_pass);
$this->db_select = mysql_select_db($this->db_name);

if($this->db_connect) {
	$this->output = 'mysql_connect() success.<br />';
}
else {
	$this->output = 'mysql_connect() failure.<br />';
}

if($this->db_select) {
	$this->output .= 'mysql_select_db() success.<br />';
}
else {
	$this->output .= 'mysql_select_db() failure.<br />';
}
echo $this->output;
    }

function disconnect() {
	$this->db_disconnect = mysql_close($this->db_connect) or die('Error closing DB Connection.<br />');

	if($this->db_disconnect) {
		$this->output = 'mysql_close() success.<br />';
	}
	else {
		$this->output = 'mysql_close() failure.<br />';
	}
	echo $this->output;
}

    function simple_num_rows($snr_from) {
	$this->snr_from = $snr_from;
	$query = "SELECT * FROM $this->snr_from";
	$result = mysql_query($query);
	$num_rows = mysql_num_rows($result);
	$this->output = $num_rows;
	return $this->output;
}

}
?>

 

<?php
// Usage example.

$swr3->database = new database;

$swr3->database->connect();

$swr3->database->simple_num_rows('members');
echo $swr3->database->output .' Members<br />';

$swr3->database->simple_num_rows('topics');
echo $swr3->database->output .' Topics<br />';

$swr3->database->simple_num_rows('replies');
echo $swr3->database->output .' Replies<br />';

$swr3->database->disconnect();
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.