How to generate page content from database the right way?

WordPress is fetching current post for you, you have to only type to the right url. When you get e.g. to example.com/lorem-ipsum/ WordPress will load automatically post with title Lorem ipsum and display it using page.php template. Your page.php template should be looking something like that.

<?php get_header(); ?>

    <div id="content">

        <?php while ( have_posts() ) : the_post(): ?>

            <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>

        <?php endwhile; ?>
    </div>

<?php
get_sidebar();
get_footer();

The concept of a loop is difficult to understand for novice developers. Only in the loop you can use functions like the_title orthe_content. Outside of the loop, these functions won’t return you anything.

https://codex.wordpress.org/The_Loop