Outputting ROW QUERY result to main program
-
Hi, I'm a newbie in OOP. Can you please help me how to fix this code below because it gives me an infinite output. I want to output or echo my fetched data to my main program. Thanks. =========================================== class DB { private $_hostdb = 'localhost'; private $_namedb = 'imsdb'; private $_userdb = 'root'; private $_passdb = ''; private $_conn; private static $_instance; private $_rowResult; private function __construct(){ try{ $this->_conn=new PDO("mysql:host=$this->_hostdb;dbname=$this->_namedb",$this->_userdb,$this->_passdb); $this->_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); if($this->_conn){ echo "Connected Successfully!
"; } } catch (Exception $ex) { echo ("Connection Failed!")."
".$ex->getMessage(); } } public static function getInstance(){ if(!isset(self::$_instance)) { return self::$_instance=new DB(); } } public function processQuery($sql){ try{ $q=$this->_conn->prepare($sql); $q->execute(); $q->setFetchMode(PDO::FETCH_ASSOC); return $this->_rowResult=$q->fetch(); //Is this correct?? } catch (Exception $ex) { echo ("Failed!")."
".$ex->getMessage(); } } } //MAIN PROGRAM -> OUTPUT gives me an infinite data which is wrong $dbUser=DB::getInstance()->processQuery("SELECT * FROM users"); while($dbUser){ echo $dbUser['username']; } -
Hi, I'm a newbie in OOP. Can you please help me how to fix this code below because it gives me an infinite output. I want to output or echo my fetched data to my main program. Thanks. =========================================== class DB { private $_hostdb = 'localhost'; private $_namedb = 'imsdb'; private $_userdb = 'root'; private $_passdb = ''; private $_conn; private static $_instance; private $_rowResult; private function __construct(){ try{ $this->_conn=new PDO("mysql:host=$this->_hostdb;dbname=$this->_namedb",$this->_userdb,$this->_passdb); $this->_conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); if($this->_conn){ echo "Connected Successfully!
"; } } catch (Exception $ex) { echo ("Connection Failed!")."
".$ex->getMessage(); } } public static function getInstance(){ if(!isset(self::$_instance)) { return self::$_instance=new DB(); } } public function processQuery($sql){ try{ $q=$this->_conn->prepare($sql); $q->execute(); $q->setFetchMode(PDO::FETCH_ASSOC); return $this->_rowResult=$q->fetch(); //Is this correct?? } catch (Exception $ex) { echo ("Failed!")."
".$ex->getMessage(); } } } //MAIN PROGRAM -> OUTPUT gives me an infinite data which is wrong $dbUser=DB::getInstance()->processQuery("SELECT * FROM users"); while($dbUser){ echo $dbUser['username']; }