title: PHP calling MySQL
body: <p>I am currently in a class at school and we have to link our MySQL database with our website using php. I already made and populated my tables in MySQL. My professor sent us this chunk of code to display the table info on our websites. However when I run it nothing happens and I haven't learned enough about php to know why it is not working. I used my correct host name, password, ect. But it won't work and when he does the tutorial online in the video it works for him.</p>
  
  <p>This is the code I am using.</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Query All Movies from Database&lt;/title&gt;
  &lt;body&gt;
  
  &lt;?
  
  @ $db = mysql_pconnect("localhost","username","password");
  
  if (!$db)
  {
      echo "ERROR: Could not connect to database.  Please try again later.";
      exit;
  }
  
  mysql_select_db("database name");
  
  $query = "select * from movie";
  
  $result = mysql_query($query);
  $num_results = mysql_num_rows($result);
  
  echo "&lt;p&gt;Number of movies found: ".$num_results."&lt;/p&gt;";
  
  for ($i=0; $i &lt; $num_results; $i++)
  {
  $row = mysql_fetch_array($result);
  echo "&lt;p&gt;";
  echo htmlspecialchars( stripslashes($row["movieid"]));
  echo "&lt;br&gt;";
  echo htmlspecialchars( stripslashes($row["title"]));
  echo "&lt;br&gt;";
  //echo htmlspecialchars( stripslashes($row["directorid"]));
  //echo "&lt;br&gt;";
  echo htmlspecialchars( stripslashes($row["year"]));
  echo "&lt;br&gt;";
  echo htmlspecialchars( stripslashes($row["genre"]));
  echo "&lt;br&gt;";
  echo htmlspecialchars( stripslashes($row["runtime"]));
  echo "&lt;br&gt;";
  echo htmlspecialchars( stripslashes($row["plotdescription"]));
  echo "&lt;br&gt;";
  echo htmlspecialchars( stripslashes($row["comments"]));
  echo "&lt;br&gt;";
  echo "&lt;/p&gt;";
  
  }  
  
  ?&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is the output I am getting.
  Directly to the screen.</p>
  
  <pre><code>Number of movies found: ".$num_results."
  "; for ($i=0; $i &lt; $num_results; $i++) { $row = mysql_fetch_array($result); echo "
  "; echo htmlspecialchars( stripslashes($row["movieid"])); echo "
  "; echo htmlspecialchars( stripslashes($row["title"])); echo "
  "; //echo htmlspecialchars( stripslashes($row["directorid"])); //echo "
  "; echo htmlspecialchars( stripslashes($row["year"])); echo "
  "; echo htmlspecialchars( stripslashes($row["genre"])); echo "
  "; echo htmlspecialchars( stripslashes($row["runtime"])); echo "
  "; echo htmlspecialchars( stripslashes($row["plotdescription"])); echo "
  "; echo htmlspecialchars( stripslashes($row["comments"])); echo "
  "; echo "
  
  "; } ?&gt;
  </code></pre>
  
  <p>I did alot of research and read the links and here is the working code! Thanks for helping teach me!! This site is so great! You guys are awesome!</p>
  
  <pre><code>&lt;?php
  $servername = "localhost";
  $username = "ursername";
  $password = "password";
  $dbname = "database name";
  
  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn-&gt;connect_error) {
      die("Connection failed: " . $conn-&gt;connect_error);
  }
  
  $sql = "SELECT movieid, title, directorid, year, genre, runtime, plotdescription, comments FROM movie";
  $result = $conn-&gt;query($sql);
  
  if ($result-&gt;num_rows &gt; 0) {
      // output data of each row
      while($row = $result-&gt;fetch_assoc()) {
          echo "Movie ID: " . $row["movieid"]. "&lt;br&gt;Title: " . $row["title"]. "&lt;br&gt;Director ID: " . $row["directorid"]. "&lt;br&gt;Year: " . $row["year"]. "&lt;br&gt;Genre: " . $row["genre"]. "&lt;br&gt;Run Time: " . $row["runtime"]. "&lt;br&gt;Plot Description: " . $row["plotdescription"]. "&lt;br&gt;Comments: " . $row["comments"]." &lt;br&gt;&lt;br&gt;";
      }
  } else {
      echo "0 results";
  }
  $conn-&gt;close();
  ?&gt;
  </code></pre>