php - Fetching Data from DB using PDO with Class -
why first sets of data displayed , not entire data?
here code..(sorry, still new oop)
include 'crud.php'; $db_host = "localhost"; $db_name = "crud"; $db_username = "root"; $db_password = ""; $conn = new pdo("mysql:host=$db_host;dbname=$db_name", $db_username, $db_password); $select = new crud($conn); $select->crud_select();
crud.php
class crud { private $conn; public function __construct($conn){ $this->conn = $conn; } public function crud_insert($lname, $fname, $address, $age){ } public function crud_select(){ $result = $this->conn->prepare("select * personal_info"); $result->execute(); $count = $result->rowcount(); if($count == 0){ $no_files = "no file(s) found!"; echo $no_files; } else{ $row = $result->fetch(); echo $row['last_name'] . "<br/>"; echo $row['first_name'] . "<br/>"; echo $row['address'] . "<br/>"; echo $row['age'] . "<br/>"; } } }
if i'm trying fetchall()
it's not displaying anything.
because have fetch 1 row. if wanted fetch multiple rows need loop
$result->execute(); $count = $result->rowcount(); $result->setfetchmode(pdo::fetch_assoc); if($count == 0){ $no_files = "no file(s) found!"; echo $no_files; } else{ while($row = $result->fetch()){ echo $row['last_name'] . "<br/>"; echo $row['first_name'] . "<br/>"; echo $row['address'] . "<br/>"; echo $row['age'] . "<br/>"; } }
or can fetch array this:
$result->execute(); $count = $result->rowcount(); if($count == 0){ $no_files = "no file(s) found!"; echo $no_files; } else{ $resultset = $datas->fetchall(pdo::fetch_assoc); foreach($resultset $row){ echo $row['last_name'] . "<br/>"; echo $row['first_name'] . "<br/>"; echo $row['address'] . "<br/>"; echo $row['age'] . "<br/>"; } }
Comments
Post a Comment