php notes - csuohio.edueecs.csuohio.edu/~sschung/cis430/php_notes.pdf · php notes this solution is...
TRANSCRIPT
PHP Notes
This solution is made using the following software components:
1. MySQL5.5 DBMS supporting an instance of the COMPANY database.
2. Microsoft WebMatrix2 web‐site creation tool.
Example 1: Retrieving Data from the Company Database
This is the entry page: index.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf‐8" /> <title>My Company ‐ PHP Page 1</title> </head> <body> <br> This button calls page phppage2.php <form action="phppage2.php" method="post" > <input type="submit" name="submit" value="Show Page2 ‐ Gender Retrieval"> </form> <p> Showing Employee data inside an HTML table </p> <h1>PHP output : Employee table</h1> <?php $con = mysql_connect("localhost","csuperson","euclid") or die('ERROR: ' . mysql_error()); mysql_select_db("company", $con); $result = mysql_query("SELECT * FROM employee"); echo "<table border='1'> <tr bgcolor='#ffff00' > <th>First Name</th> <th>Last Name</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['fname'] . "</td>"; echo "<td>" . $row['lname'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?> <h1>Employees working for Dept 4 </h1> <?php $host="127.0.0.1"; $port=3306; $socket=""; $user="csuperson"; $password="euclid"; $dbname="company"; $con = new mysqli($host, $user, $password, $dbname, $port, $socket) or die ('Could not connect to the database server' . mysqli_connect_error()); ?>
<b> after connection</b> <?php $query = "SELECT fname, lname, dno FROM employee WHERE dno = 4"; if ($stmt = $con‐>prepare($query)) { $stmt‐>execute(); $stmt‐>bind_result($field1, $field2, $field3); while ($stmt‐>fetch()) { printf("<br> %s %s %d ", $field1, $field2, $field3); } $stmt‐>close(); } $con‐>close(); ?> </body> </html>
This is page: PHPPage3.php
<html> <head> <title>My Company ‐ PHP Page 3</title> </head> <?php // Capture the values sent by phppage2.php $YourName = $_REQUEST['YourName'] ; $Gender = $_REQUEST['Gender'] ; ?> <body > <?php print "<p> Welcome ..... $YourName "; print "<p> You indicated SEX search value ... $Gender "; print "<h1>Using mySQL generated script </h1> "; ?>
<?php $host="127.0.0.1"; $port=3306; $socket=""; $user="csuperson"; $password="euclid"; $dbname="company"; $con = new mysqli($host, $user, $password, $dbname, $port, $socket) or die ('Problems ‐ ' . mysqli_connect_error()); $query = "SELECT fname, lname, dno FROM employee WHERE sex = '" . $Gender . "'"; print "<P> $query <br>"; $stmt2 = $con‐>prepare($query); if ($stmt2 != NULL ) { $stmt2‐>execute(); $stmt2‐>bind_result($field1, $field2, $field3); while ($stmt2‐>fetch()) { printf("<br> %s %s %d ", $field1, $field2, $field3); } $stmt2‐>close(); } else { print "<br> FALSE "; } $con‐>close(); ?> </body> </html>