Jump to content

Database Class Help


drseuss

Recommended Posts

Hello, I am new to OOP. I am trying to make a Database Class, which was going well up to the testing period. When I run the test, the script runs without parse errors or anything, but the output never stops, kind of like the script is in an infinite loop.

 

Oh and if you plan to bitch and moan about me being new to OOP coding, have at it. Just remember that it shows how immature a person you are.

 

Thank you in advanced.

 

database.class.php

<?PHP

class Database {

var $host;
var $user;
var $pass;
var $selectDb;

var $connection;
var $Db;

var $query;
var $error;

function __construct() {

	$this->host = 'localhost';
	$this->user = 'dwsonet';
	$this->pass = 'p1a2s3s4';
	$this->selectDb = 'dwsonet_game';

	$this->connect();

}

function connect() {

	$this->connection = mysql_connect($this->host, $this->user, $this->pass);

	if(!$this->connection) {

		$this->error = mysql_error();
		die('Can not continue');

	}

	$this->Db = mysql_select_db($this->selectDb,$this->connection);

	if(!$this->Db) {

		$this->error = mysql_error();
		die('Can not continue');

	}

}

function query($sql) {

	$this->query = mysql_query($sql)or die(mysql_error());
	return $this->query;

}

function fetch() {

	return mysql_fetch_array($this->query)or die(mysql_error());

}

}

 

test.php (I made it simple to show how I am using it. Please correct me if I am wrong)

 

<?PHP
error_reporting(E_ALL);
include 'lib/database.class.php';
$db = new Database();

$query = 'select * from accounts';
while ($a = $db->fetch($db->query($query))){

echo $a['username']."<br />".$a['password'];

}

?>

Link to comment
https://forums.phpfreaks.com/topic/199092-database-class-help/
Share on other sites

Okay, I will bitch about it lol.

 

1. NEVER, and I literally mean NEVER, give away your username and password. I sure hope those data are wrong. But next time, hide them or use some dummy values.

 

2. The problem lies in your while loop, more so how you're running it. See, every time through the loop, you're running the $db->query($query) line, which runs a fresh new query, which means you will never get passed the first entry because the second iteration through the loop, you will run SELECT * FROM accounts again. You want to run a query one time, but fetch the results more than once. ;)

Link to comment
https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1044993
Share on other sites

I didn't change a lot. And it does not error out.

<?PHP
error_reporting(E_ALL);
include 'lib/database.class.php';
$db = new Database();

$query = $db->query('select * from accounts');
while ($a = $db->fetch($query)){

echo $a['id'].$a['username']."<br />".$a['password'];

}

?>

Link to comment
https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1045005
Share on other sites

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.