Displaying a random poem from a database I'm new to web development, and I'm constantly torn between good code and bad code. I was wondering if you could help me improve my following code and see if I'm doing it right in terms of how I use it on the line by line basis: - t code checks to see if a cookie exists if it doesnt exist it sets a cookie to last for 23 hours and 30 minutes after the cookie is set it pulls a random poem from a database using and and sets it on page to be displayed if the user visits the page more than once and the cookie is set no data will be retrieved and leave old data till the cookie expires -

&lt;?php

if (@!$_COOKIE['true']){
    setcookie('true','true',time()+86100);

    $timeCheck = date('H:i', strtotime('23:30:00'));

    $currentTime = date('H:i', time());
    if ($currentTime &gt;= $timeCheck){
            require_once 'upload/mysqlserver/login.php';

    $query = "SELECT * FROM poems";
    $queryRun = mysql_query($query);
     if ($queryRun = mysql_query($query)){
                // getting data
                if (mysql_num_rows($queryRun)== NULL){
                   echo "No poems in DataBase";
                }else{
                    $randNum = mysql_num_rows($queryRun);
                    $maxNum = rand(1, $randNum);

                    $query2 = "SELECT poem_name, poem, author, time_posted FROM poems WHERE id=$maxNum";
                    $queryRun2 = mysql_query($query2);

                        $row = mysql_fetch_assoc($queryRun2);
                        $poemName = $row['poem_name'];
                        $poem = $row['poem'];
                        $author = $row['author'];
                        $time_posted = $row['time_posted'];

                        $PoTDset = $poemSet = "&lt;br /&gt;
                        &lt;div class='allContent'&gt;
                        &lt;div class='poem'&gt;
                        &lt;h1&gt;Poem name:&lt;/h1&gt;&lt;h2&gt;".$poemName ."&lt;/h2&gt;
                        &lt;br /&gt;"
                        ."&lt;h2&gt;Poem:&lt;/h2&gt;&lt;br /&gt;"."&lt;p class='poemScroll'&gt;".$poem."&lt;/p&gt;
                        &lt;br /&gt;&lt;br /&gt;"
                        ."&lt;h3&gt;Author:&lt;/h3&gt; ".$author."&lt;br /&gt;"
                        ."&lt;h3&gt;Posted on: &lt;/h3&gt;".$time_posted."&lt;/div&gt;&lt;/div&gt;";

                        $handle = fopen('navigation/PoTD/setPoTD.php', 'w');
                        fwrite($handle, "&lt;?php"."\n"."return ".'"'.$PoTDset.'"'."\n".";\n?&gt;");
                        @fclose($handle);    

                 }
            }
        }
}

$pageTemp-&gt;content .= include_once "navigation/PoTD/setPoTD.php";
?&gt;
In addition to the depreciation and injection issues pointed out in the comments, I would just add that SELECT * FROM poems will fetch all columns for all rows, which is terribly inefficient. SELECT COUNT(*) FROM poems will return the number of records in the table, and you won't have to call mysql_num_rows.