Thanks Ashwin for giving me ideas. I really appreciate it. I have managed to fix it.Please see my code below for main program. ===================================== $dbUser=DB::getInstance(); $dbUser->processQuery("SELECT * FROM users"); echo count($dbUser->getResultSet()); foreach($dbUser->getResultSet() as $rows){ echo $rows['username']; }
ELMAGLAYA
Posts
-
Why my fetch row method just repeats the first record multiple times (same count as my number of fields in the table) -
Why my fetch row method just repeats the first record multiple times (same count as my number of fields in the table)Can you help me please what is the proper way. Thanks.
-
Why my fetch row method just repeats the first record multiple times (same count as my number of fields in the table)I've tried code this below but it gives me one record and it repeats 7 times. Wrong output. Can anyone has a good heart to figure it out please. Please see my complete code below. I'm just a newbie in programming. Sorry. Thank you. =================================== OUTPUT: 1111111 =================================== _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(); } catch (Exception $ex) { return ("Failed!")."
".$ex->getMessage(); } } public function getResultSet(){ return $this->rowResult; } //MAIN PROGRAM $dbUser=DB::getInstance(); $user=$dbUser->processQuery("SELECT * FROM users"); for($i=0;$irowResult);$i++){ echo $user['id']; } ?> -
How to fix this error $_SESSION:Notice: Undefined offset: 0Thank you sir.
-
Outputting ROW QUERY result to main programHi, 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']; } -
How to fix this error $_SESSION:Notice: Undefined offset: 0Hi sorry I am just a baby in PHP. I want to get the value of $_SESSION['username'] but it gives me this error : Notice: Undefined offset: 0. Please see my code below. Your help will be greatly appreciated. Thanks. "; echo $_SESSION[0]; //I want an output "andrew" but it fails.