I'm using PDO to connect the database and using OOP method in coding
this is how to get the posts and comments
class MyWeb{
public function SelectStatus($user_id){
try{
$DBC = new DBConnector();
$query = "SELECT * FROM users U, posts P where P.user_id_fk=U.user_id and U.user_id=:user_id_fk";
$params = array(":user_id_fk"=>$user_id);
$result = $DBC->SelectArray($query,$params);
if($result){
return $result;
} else throw new Exception("Post not selected!");
}catch(Exception $e){
echo "Caught Exception: ".$e->getMessage();
return null;
}
}
public function SelectComment($post_id){
try{
$DBC = new DBConnector();
$query = "SELECT * FROM comments C, users U WHERE C.user_id_fk = U.user_id and C.post_id_fk = :post_id_fk";
$params = array(":post_id_fk"=>$post_id);
$result = $DBC->SelectArray($query,$params);
if($result){
return $result;
} else throw new Exception("Comment not selected!");
}catch(Exception $e){
echo "Caught Exception: ".$e->getMessage();
return null;
}
}
}
and this how to call the functions and display posts and comments
<?php
$NewStatus = $session->SelectStatus($user_id);
if(!empty($NewStatus)){
foreach($NewStatus as $data){
$username = $data->username;
$post = $data->post;
$post_id = $data->post_id;
echo "".$username." | ".$post."";
$NewComment = $session->SelectComment($post_id);
if(!empty($NewComment)){
foreach($NewComment as $cdata){
echo $cdata->comment;
}
}
}
}
?>
But sadly I always get error -> Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\RIO\RIO\RAI\session_rai\includes\db.php on line 14
So, Any solutions for this case? Thanks.
Your class structure is wrong.
So, create a PDO connection, then instantiate MyWeb class and then get your data
class MyWeb{
function __construct($dbc)
{
$this->dbc = $dbc;
}
public function SelectStatus($user_id)
{
$query = "SELECT * FROM users U, posts P
WHERE P.user_id_fk=U.user_id and U.user_id=?";
$stmt = $this->dbc->prepare($query);
$stmt->execute(array($user_id));
return $stmt->fetchAll();
}
public function SelectComment($post_id)
{
$query = "SELECT * FROM comments C, users U
WHERE C.user_id_fk = U.user_id and C.post_id_fk = ?";
$stmt = $this->dbc->prepare($query);
$stmt->execute(array($user_id));
return $stmt->fetchAll();
}
}
same with output
<?php
$pdo = new PDO(... params);
$myweb = new MyWeb($pdo);
$NewStatus = $myweb->SelectStatus($user_id);
foreach($NewStatus as $row)
{
echo $row['username']." | ".$row['post'];
$NewComment = $myweb->SelectComment($post_id);
foreach($NewComment as $cdata){
echo $cdata['comment'];
}
}
OR
if you make selectArray
function this way
public function selectArray()
{
$args = func_get_args();
$sql = array_shift($args);
$stmt = $this->pdo->prepare($sql);
$stmt->execute($args);
return $stmt->fetchAll();
}
you can save yourself a line or two:
public function SelectStatus($user_id)
{
$query = "SELECT * FROM users U, posts P
WHERE P.user_id_fk=U.user_id and U.user_id=?";
return $this->dbc->selectArray($query, $user_id);
}
You have a syntax error here
$query = "SELECT * FROM comments C, users U WHERE C.user_id_fk = U.user_id and C.post_id_fk = :post_id_fk";";
While it should be
$query = "SELECT * FROM comments C, users U WHERE C.user_id_fk = U.user_id and C.post_id_fk = :post_id_fk";