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. 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)

Scheduled Pinned Locked Moved Web Development
databasemysqlquestion
6 Posts 3 Posters 1 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

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

    A C 2 Replies Last reply
    0
    • E ELMAGLAYA

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

      A Offline
      A Offline
      Ashwin Shetty
      wrote on last edited by
      #2

      I can see your for-loop influencing $user object

      for($i=0;$irowResult);$i++){
      echo $user['id'];
      }

      Ashwin Shetty

      E 1 Reply Last reply
      0
      • A Ashwin Shetty

        I can see your for-loop influencing $user object

        for($i=0;$irowResult);$i++){
        echo $user['id'];
        }

        Ashwin Shetty

        E Offline
        E Offline
        ELMAGLAYA
        wrote on last edited by
        #3

        Can you help me please what is the proper way. Thanks.

        A 1 Reply Last reply
        0
        • E ELMAGLAYA

          Can you help me please what is the proper way. Thanks.

          A Offline
          A Offline
          Ashwin Shetty
          wrote on last edited by
          #4

          if you need to read data (forward only), you can write code like

          $sql = "SELECT id, firstname, lastname FROM MyGuests";
          $result = $conn->query($sql);

          if ($result->num_rows > 0) {
          // output data of each row
          while($row = $result->fetch_assoc()) {
          echo "id: " . $row["id"];
          }
          } else {
          echo "0 results";
          }
          $conn->close();

          you can look at the sample available here

          Ashwin Shetty

          E 1 Reply Last reply
          0
          • A Ashwin Shetty

            if you need to read data (forward only), you can write code like

            $sql = "SELECT id, firstname, lastname FROM MyGuests";
            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
            // output data of each row
            while($row = $result->fetch_assoc()) {
            echo "id: " . $row["id"];
            }
            } else {
            echo "0 results";
            }
            $conn->close();

            you can look at the sample available here

            Ashwin Shetty

            E Offline
            E Offline
            ELMAGLAYA
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • E ELMAGLAYA

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

              C Offline
              C Offline
              COBOLdinosaur
              wrote on last edited by
              #6

              You are using fetch() which will not return anything but the first row. Try return $this->rowResult=$q->fetchAll(); That will then return an array of the rows into $users which I think is what you were expecting.

              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