|
$arrayVar = mysqli_fetch_row ( $result ) |
|
($result is the output of mysqli_query( ) I.e.: $result = mysqli_query( $conn, "SQL-command" ) |
<?php
# --------------------------------
# 1. Connect and select DB
#---------------------------------
$conn = mysqli_connect("holland.mathcs.emory.edu", "cs377", "abc123",
"companyDB");
if ( mysqli_connect_errno( ) != 0 )
{
/* -----------------------------------------
Error code > 0 means there was an error
----------------------------------------- */
printf("Connect failed: %s\n", mysqli_connect_error());
exit(1);
}
# --------------------------------
# 2. Execute a query
# --------------------------------
$query = 'select fname, lname, salary from employee';
if ( ( $result = mysqli_query($conn, $query) ) == NULL )
{
printf("Error: %s\n", mysqli_error($conn));
exit(1);
}
# --------------------------------
# 3. Fetch the tuples
# --------------------------------
while ( ( $row = mysqli_fetch_row( $result ) ) != NULL )
{
for ( $i = 0; $i < count($row); $i++ )
{
printf($row[$i] . "\t"); // \t is TAB to make output "pretty"
}
print("\n");
}
# NOTE: We must still:
#
# 1. free resource and
# 2. close connection
#
# This will be discussed LATER !
?>
|
Sample output: ('select fname, lname, salary from employee')
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 |
Login to holland.mathcs.emory.edu Run: php /home/cs377001/PHP/intro-PHP/employee.php Also, try this version that prints $result and $row using print_r( ): php /home/cs377001/PHP/intro-PHP/employee1.php |