|
<html>
<head>
<title> MySQL Test 1</title>
</head>
<body>
<H3>
<HR>
Accessing a Database through a <I>Web browser</I> using PHP
<HR>
</H3>
<P>
<UL>
<?php
$conn = mysqli_connect("holland.mathcs.emory.edu","cs377", "abc123", "companyDB");
if (mysqli_connect_errno()) # ----------- check connection error
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit(1);
}
$query = 'select fname, lname, salary from employee';
if ( ( $result = mysqli_query($conn, $query)) == NULL ) # Execute query
{
printf("Error: %s\n", mysqli_error($conn));
exit(1);
}
printf("Select returned %d rows.\n", mysqli_num_rows($result));
# ------------------------------------------------------------
# Print names of attributes
# ------------------------------------------------------------
while ( ($field_details = mysqli_fetch_field($result)) != NULL )
{
print $field_details->name . "\t";
}
print "\n";
print "========================================\n";
# ------------------------------------------------------------
# Print the tuples
# ------------------------------------------------------------
while ( ($row = mysqli_fetch_row( $result ) ) != NULL )
{
for ( $i = 0; $i < count($row); $i++ )
{
print ($row[$i] . "\t");
}
print ("\n");
}
mysqli_free_result($result);
mysqli_close($conn);
?>
</UL>
<P>
<HR>
<HR>
</body>
</html>
|
/home/cs377001/public_html/Web/employee.php |
php /home/cs377001/public_html/Web/employee.php
|
Output: a web page !!!
<html> <head> <title> MySQL Test 1</title> </head> <body> <H3> <HR> Accessing a Database through a <I>Web browser</I> using PHP <HR> </H3> <P> <UL> Select returned 8 rows. fname lname salary ======================================== John Smith 30000.00 Frankl Wong 40000.00 Alicia Zelaya 25000.00 Jennif Wallace 43000.00 Ramesh Narayan 38000.00 Joyce English 25000.00 Ahmad Jabbar 25000.00 James Borg 55000.00 </UL> <P> <HR> <HR> <HR> <HR> <HR> </body> </html> |
Enter this URL in a web browser:
http://holland.mathcs.emory.edu/~cs377001/Web/employee.php |
(You can right click in the web browser and select View Page Source to see the output)