Difference between revisions of "HTML/PHP Database Dump"

From Pathology Education Instructional Resource
Jump to: navigation, search
(Created page with "<?php $con = mysqli_connect("computer", "user", "password", "database"); //This references the database. We store it as a variable only so that we don't have to keep writing i...")
 
Line 1: Line 1:
<?php
+
<pre>
$con = mysqli_connect("computer", "user", "password", "database"); //This references the database. We store it as a variable only so that we don't have to keep writing it out.  
+
&lt;?php
 +
$con = mysqli_connect("localhost", "gbs788db", "pw", "gbs788db"); //This references the database. We store it as a variable only so that we don't have to keep writing it out.  
 
if(mysqli_connect_errno())
 
if(mysqli_connect_errno())
 
{
 
{
Line 8: Line 9:
 
/*If we made it this far, it means that we successfully connected and made it to the next line. By putting echo, we are including html now!!*/
 
/*If we made it this far, it means that we successfully connected and made it to the next line. By putting echo, we are including html now!!*/
 
echo '
 
echo '
<html>
+
&lt;html&gt;
   <head>
+
   &lt;head&gt;
     <title>Ooga</title>
+
     &lt;title&gt;Ooga&lt;/title&gt;
   </head>
+
   &lt;/head&gt;
   <body>
+
   &lt;body&gt;
     <h1>It is Tims Fault</h1>
+
     &lt;h1&gt;It is Tims Fault&lt;/h1&gt;
     <table border="1">
+
     &lt;table border="1"&gt;
       <tr>
+
       &lt;tr&gt;
         <th>Accession</th>
+
         &lt;th&gt;Accession&lt;/th&gt;
         <th>Ordered By</th>
+
         &lt;th&gt;Ordered By&lt;/th&gt;
         <th>Test</th>
+
         &lt;th&gt;Test&lt;/th&gt;
         <th>Turn Around Time</th>
+
         &lt;th&gt;Turn Around Time&lt;/th&gt;
         <th>Result</th>
+
         &lt;th&gt;Result&lt;/th&gt;
       </tr>
+
       &lt;/tr&gt;
 
';
 
';
$sql = "SELECT * FROM `table`;";
+
$sql = "SELECT * FROM `test2`;";
 
$result = mysqli_query($con, $sql); //This does what we have been doing in SQL: connecting to the database, and then putting in the query.
 
$result = mysqli_query($con, $sql); //This does what we have been doing in SQL: connecting to the database, and then putting in the query.
 
while ($row=mysqli_fetch_array($result))//This command just looks at a table row by row.
 
while ($row=mysqli_fetch_array($result))//This command just looks at a table row by row.
Line 29: Line 30:
 
{
 
{
 
echo '
 
echo '
       <tr>
+
       &lt;tr&gt;
         <td>' . $row['col1'] . '</td>
+
         &lt;td&gt;' . $row['accession'] . '&lt;/td&gt;
         <td>' . $row['col2'] . '</td>
+
         &lt;td&gt;' . $row['ordering'] . '&lt;/td&gt;
         <td>' . $row['col3'] . '</td>
+
         &lt;td&gt;' . $row['test'] . '&lt;/td&gt;
         <td>' . $row['col4'] . '</td>
+
         &lt;td&gt;' . $row['tat'] . '&lt;/td&gt;
         <td>' . $row[''] . '</td>
+
         &lt;td&gt;' . $row['result'] . '&lt;/td&gt;
       </tr>  
+
       &lt;/tr&gt;  
 
'; /*This closes out the echo*/
 
'; /*This closes out the echo*/
 
}
 
}
 
echo '
 
echo '
     </table>
+
     &lt;/table&gt;
   </body>
+
   &lt;/body&gt;
</html>
+
&lt;/html&gt;
 
';
 
';
?>
+
?&gt;
 +
</pre>

Revision as of 17:28, 23 October 2013

<?php
$con = mysqli_connect("localhost", "gbs788db", "pw", "gbs788db"); //This references the database. We store it as a variable only so that we don't have to keep writing it out. 
if(mysqli_connect_errno())
{
	die("Could not connect: " . mysqli_connect_errno());//Kills the connection if an error and uses mysqli_connect_errno () to print a readable message to the screen
	/*The error number will pass as a zero if it is successful; otherwise it will include an error number. The "." is a concatenator. */
}
/*If we made it this far, it means that we successfully connected and made it to the next line. By putting echo, we are including html now!!*/
echo '
<html>
  <head>
    <title>Ooga</title>
  </head>
  <body>
    <h1>It is Tims Fault</h1>
    <table border="1">
      <tr>
        <th>Accession</th>
        <th>Ordered By</th>
        <th>Test</th>
        <th>Turn Around Time</th>
        <th>Result</th>
      </tr>
';
$sql = "SELECT * FROM `test2`;";
$result = mysqli_query($con, $sql); //This does what we have been doing in SQL: connecting to the database, and then putting in the query.
while ($row=mysqli_fetch_array($result))//This command just looks at a table row by row.
/*This while loop allows the computer to keep going back in and getting another result*/
{
	echo '
      <tr>
        <td>' . $row['accession'] . '</td>
        <td>' . $row['ordering'] . '</td>
        <td>' . $row['test'] . '</td>
        <td>' . $row['tat'] . '</td>
        <td>' . $row['result'] . '</td>
      </tr>  
	'; /*This closes out the echo*/
}
echo '
    </table>
  </body>
</html>
';
?>