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']; } ?> -
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']; } ?>I can see your for-loop influencing $user object
for($i=0;$irowResult);$i++){
echo $user['id'];
}Ashwin Shetty
-
I can see your for-loop influencing $user object
for($i=0;$irowResult);$i++){
echo $user['id'];
}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
-
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
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']; }
-
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']; } ?>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.