Skip to content

Instantly share code, notes, and snippets.

@shadda
Created July 8, 2015 06:20
Show Gist options
  • Select an option

  • Save shadda/8329cf3665e61e0d0113 to your computer and use it in GitHub Desktop.

Select an option

Save shadda/8329cf3665e61e0d0113 to your computer and use it in GitHub Desktop.
<?php
$db = new PDO("mysql:dbname=mydb;host=localhost", $user, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
function get_row($parent_id)
{
global $db; //I would normally never do this, but its simpler than explaining the concept of a singleton
$parent_id = (int) $parent_id; //Cast to int
$q = $db->prepare("
SELECT
posts.post_topic, posts.post_content, posts.post_id, posts.post_date, posts.file_uploaded,
posts.post_by, posts.post_parent, users.user_id, users.user_name, users.user_pic
FROM
posts
LEFT JOIN users ON posts.post_by = users.user_id
WHERE
posts.topic_id = :parent_id
");
$q->execute(
array(
':parent_id' => $parent_id
)
);
while($row = $q->fetch(PDO::FETCH_OBJ))
{
if($row->parent_id != NULL)
{
get_row($row->parent_id);
}
//Do other shit with $row here
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment