Reputation: 19
I'm a newbie in PHP 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'];
}
Upvotes: 0
Views: 54
Reputation: 365
Your while loop is infinite because it keeps processing $dbUser which returns true each time, it's equivalent to saying while(true)
You really want to access the result set via a getter method, so in your class you could add something like:
public function getResultSet() {
return $this->_rowResult;
}
I've edited this and modified the bits you have listed:
class DB {
...
public static function getInstance()
{
if (null === self::$_instance)) {
self::$_instance = new DB();
}
return self::$_instance; // Always return your instance, the if statement just sets it in the property if it's not already
}
public function processQuery($sql)
{
try {
$q = $this->_conn->prepare($sql);
$q->execute();
$q->setFetchMode(PDO::FETCH_ASSOC);
$this->rowResult = $q->fetchAll(); // Use fetchAll rather than fetch
} catch (Exception $ex) {
echo "Failed!", "<br />", $ex->getMessage();
}
}
...
}
And your main program:
//MAIN PROGRAM
$dbUser = DB::getInstance();
$user = $dbUser->processQuery("SELECT * FROM users");
foreach ($dbUser->getResultSet() as $user) {
print_r($user['id']);
}
Upvotes: 1
Reputation: 19
It tried code below but it gives me one record and it repeats 7 times.
OUTPUT: 1111111
===================================
class DB {
private $_hostdb = 'localhost';
private $_namedb = 'imsdb';
private $_userdb = 'root';
private $_passdb = '';
private $_conn;
private static $_instance; //must be static
public $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!<br />";
}
//
} catch (Exception $ex) {
echo ("Connection Failed!")."<br />".$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();
} catch (Exception $ex) {
return ("Failed!")."<br />".$ex->getMessage();
}
}
public function getResultSet(){
return $this->rowResult;
}
}
//MAIN PROGRAM
$dbUser=DB::getInstance();
$user=$dbUser->processQuery("SELECT * FROM users");
for($i=0;$i<count($dbUser->rowResult);$i++){
echo $user['id'];
}
Upvotes: 1