Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Outputting ROW QUERY result to main program

Outputting ROW QUERY result to main program

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasehelpmysqltutorialquestion
2 Posts 2 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ELMAGLAYA
    wrote on last edited by
    #1

    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']; }

    L 1 Reply Last reply
    0
    • E ELMAGLAYA

      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']; }

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      while($dbUser){
      echo $dbUser['username'];
      }

      The while statement will repeat as long as the value in $dbUser is not null. So in your case it will continue until the end of time.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups