drseuss Posted April 20, 2010 Share Posted April 20, 2010 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/ Share on other sites More sharing options...
Ken2k7 Posted April 20, 2010 Share Posted April 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1044993 Share on other sites More sharing options...
drseuss Posted April 20, 2010 Author Share Posted April 20, 2010 Yea thats the credentials I use on my home comp (xxamp haha). I'd never give my info out lol I understand where you are coming from, but I cannot think of how to make the $this->query() run only once while the results keep going. Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1044996 Share on other sites More sharing options...
Ken2k7 Posted April 20, 2010 Share Posted April 20, 2010 Just run it outside the while loop. Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1044998 Share on other sites More sharing options...
drseuss Posted April 20, 2010 Author Share Posted April 20, 2010 Well it doesn't loop non-stop anymore. But it still wont display results. Thanks for your help, I guess I didn't fully understand how the loop works. Lol Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1045002 Share on other sites More sharing options...
Ken2k7 Posted April 20, 2010 Share Posted April 20, 2010 Please post the updated code. Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1045003 Share on other sites More sharing options...
drseuss Posted April 20, 2010 Author Share Posted April 20, 2010 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']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1045005 Share on other sites More sharing options...
Ken2k7 Posted April 20, 2010 Share Posted April 20, 2010 $db->fetch() doesn't take any parameters. Just take out $query. Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1045010 Share on other sites More sharing options...
drseuss Posted April 20, 2010 Author Share Posted April 20, 2010 All is well now. Thanks a lot for you help. Now I continue testing. Quote Link to comment https://forums.phpfreaks.com/topic/199092-database-class-help/#findComment-1045013 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.