<?php
/* -----------------------------------------------
Make connection to SQL server (and select DB)
----------------------------------------------- */
$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);
}
/* ----------------------------------------
Send query to SQL server for execution
----------------------------------------- */
$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\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);
?>
|
Output:
Select returned 8 rows. fname lname salary --------------------------------- John Smith 50000.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 |
How to run the program:
Login to holland.mathcs.emory.edu
Run: php /home/cs377001/PHP/intro-PHP/employee2.php
|