Am new to PDO statements in PHP. Am having trouble in writing query to implement pdo using oops concepts. The goal I wish to achieve is a reusable select function.
public function selectpackage($table) {
try {
$query = $this->db->prepare("select * from $this->table");
return $query->fetch(PDO::FETCH_ASSOC);
}
catch(PDOException $e)
{
echo 'Query failed'.$e->getMessage();
}
}
$db
is used for connecting database. Now, in the page where am calling this function, I have written
$result = $obj->selectpackage("tbl_packages");
while($row = $result) {
echo $row['title']."</br>";
}
Its not giving the results. Does anyone know where the issue is ?
l have checked Siraj code , its working
class oopCrud
{
private $host="localhost";
private $user="root";
private $db="databasename";
private $pass="";
private $conn;
public function __construct(){
$this->conn = new PDO("mysql:host=".$this->host.";dbname=".$this->db,$this->user,$this->pass);
}
public function showData($table){
try {
$sql="SELECT * FROM $table";
$q = $this->conn->query($sql) or die("failed!");
while($r = $q->fetch(PDO::FETCH_ASSOC)){ $data[]=$r; }
return $data;
}
catch(PDOException $e)
{
echo 'Query failed'.$e->getMessage();
}
}
}
$obj=new oopCrud;
$result = $obj->showData("tablename");
print_r($result);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.