title: Using cron job to change a PHP variable with MySQL SELECT query every month
body: <p>Okay so I have many scattered pieces of the puzzle. I am trying to make an application that updates the quote at the bottom of my website every month (ie 'Quote of the Month'). I am using PHP to access the MySQL server, and the MySQL server has the quotes in them. </p>
  
  <p>In the database, the table is called 'quotes' and has columns 'quote_id', 'quotation', 'author', and 'datetime_used'. The 'datetime_used' column has a default value of 1991 and I use it to check whether I have used that quote in the last year. If the randomly chosen quote's time is less than the current time minus 1 year, then that quote is used as the new quote of the month. If the quote is used, the 'datetime_used' is updated to current time. </p>
  
  <p>The steps I would think to go through is to:
  1. Run a cron job (which would run every month) that does runs the following PHP script:</p>
  
  <pre><code>&lt;?php
  //Make connection
  $connect = mysqli_connect("localhost","user","password","dbname");
  
  //Check connection
  if (mysqli_connect_errno($connect))
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
  
  //Run query
  //Select random quote
  $result = mysqli_query($connect,
  "SELECT quotation, author FROM quotes 
      WHERE datetime_used &lt;= NOW() - INTERVAL 1 YEAR 
      ORDER BY RAND() 
      LIMIT 1");
  
  $row = $mysqli_fetch_array($result);
  $quote = $row['quotation'];
  
  //Update the datetime_used column to NOW()
  mysqli_query($connect,"
  UPDATE quotes SET datetime_used=NOW() 
      WHERE quotation='$quote'");
  
  mysqli_close($connect);
  ?&gt;
  </code></pre>
  
  <p>2. Then include the $quote generated by the cron job script above into my website code:</p>
  
  <pre><code>&lt;footer&gt;
  &lt;p&gt;Quote of the month: 
  &lt;?php
  //INCLUDE THE $quote SOMEHOW
  echo $quote;
  ?&gt;
  &lt;/p&gt;
  &lt;/footer&gt;
  </code></pre>
  
  <p>So my question is how do I make the cron job update the $quote variable in a way so that I can include the updated variable into my website code. Or is there a better way to do this?</p>
  
  <p>EDIT: The code I came up with that seems to work.</p>
  
  <pre><code>&lt;?php
  //Make connection
  $connect = mysqli_connect("localhost","root","root","test");
  
  //Check connection
  if (mysqli_connect_errno($connect)) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  //Check for month difference between this month and most recently used quote as well as get the quote and author to store
  $check = mysqli_query($connect, 'SELECT MONTH(NOW())-MONTH(datetime_used) AS m, quotation, author FROM quotes ORDER BY datetime_used  DESC LIMIT 1');
  $checkarray = mysqli_fetch_array($check);
  $quote = $checkarray['quotation'];
  $author = $checkarray['author'];
  //If the month difference if 1 or greater, it selects a new quote
  if ($checkarray['m']) {
      $result = mysqli_query($connect,
      "SELECT quotation, author FROM quotes 
          WHERE datetime_used &lt;= NOW() - INTERVAL 1 YEAR 
          ORDER BY RAND() 
          LIMIT 1");
  
      $row = mysqli_fetch_array($result);
      $quote = $row['quotation'];
      $author = $row['author'];
  
      //Update the datetime_used column to NOW()
      mysqli_query($connect,
      "UPDATE quotes SET datetime_used=NOW() 
          WHERE quotation='$quote' AND author='$author'");
  }
  
  mysqli_close($connect);
  
  echo "\"$quote\"
  &lt;/cite&gt;
  -$author";
  ?&gt;
  </code></pre>