title: How to check if MySQL table is empty or not when I reload page PHP
body: <p>I work on a CRUD PHP page and everything works perfectly. The data that I take is from JSON url (<a href="https://jsonplaceholder.typicode.com/todos/" rel="nofollow noreferrer">https://jsonplaceholder.typicode.com/todos/</a>).</p>
  
  <p>How can I check and populate MySQL table if is empty every time when i load my index page.  </p>
  
  <p>This my PHP code how I connect to database. And function to inset JSON data to MySQL(I'm not sure that works correctly) :</p>
  
  <pre><code>class Database
  {
      private $db_host;
      private $db_name;
      private $db_username;
      private $db_password;
  
      public function dbConnection()
      {
          $this-&gt;db_host = 'localhost';
          $this-&gt;db_name = 'items';
          $this-&gt;db_username = 'root';
          $this-&gt;db_password = '';
          try {
              $conn = new PDO('mysql:host=' . $this-&gt;db_host . ';dbname=' . $this-&gt;db_name, $this-&gt;db_username, $this-&gt;db_password);
              $conn-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
              return $conn;
          } catch (PDOException $e) {
              echo "Connection error " . $e-&gt;getMessage();
              exit;
          }
      }
  }
  </code></pre>
  
  <p>Function <code>getDati()</code> that works correctly and returns array with 200 objects</p>
  
  <pre><code>function getDati()
  {
      $url = 'https://jsonplaceholder.typicode.com/todos/';
      $cURL = curl_init();
      curl_setopt($cURL, CURLOPT_URL, $url);
      curl_setopt($cURL, CURLOPT_HTTPGET, true);
      curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
  
      curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
          'Content-Type: application/json',
          'Accept: application/json'
      ));
  
      $result = curl_exec($cURL);
      curl_close($cURL);
  
      $all_items = json_decode($result);
  
      return $all_items;
  }
  
  function intoDB()
  {
      $database = new Database;
      $db = $database-&gt;dbConnection();
      $all_items = getDati();
  
      $sql = "SELECT count(*) FROM posts ";
      $result = $db-&gt;prepare($sql);
      $result-&gt;execute();
      $number_of_rows = $result-&gt;fetchColumn();
      if ($number_of_rows &lt;= 0) {
          foreach ($all_items as $k =&gt; $item) {
  
              $sql = "INSERT INTO posts (id, userId, title, completed) VALUES (?,?,?,?)";
              $stmt = $db-&gt;prepare($sql);
              $stmt-&gt;execute([$item-&gt;id, $item-&gt;userId, $item-&gt;title, $item-&gt;completed]);
          }
      }
  }
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&gt;
      &lt;title&gt;Documents&lt;/title&gt;
      &lt;link rel="stylesheet" href="./style.css"&gt;
      &lt;script src="https://kit.fontawesome.com/12883fd222.js" crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous"&gt;
      &lt;link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css"&gt;
      &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
      &lt;link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"&gt;&lt;/script&gt;
      &lt;script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  
  &lt;?php
  require_once 'process.php';
  intoDB();  // this is where i call the function.
  </code></pre>
  
  <p>It should be <code>IF</code> that will check if table is full or empty but I donk know how to do that. </p>
  
  <p>When I recall  <code>intoDB()</code> nothing happens. MySQL table remain empty. I want to check every time when I reload or open index page that my MySQL table is populated and if it is empty to populate it. Any suggestion how to do that? Thanks in advance!</p>