0% found this document useful (0 votes)
9 views

Create Connection

This PHP script connects to a MySQL database called "myDB", selects the coffee inventory table, queries for coffee name, roast type and quantity ordered by quantity descending, and displays the results in an HTML table. It then closes the database connection.

Uploaded by

anup_it2010
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Create Connection

This PHP script connects to a MySQL database called "myDB", selects the coffee inventory table, queries for coffee name, roast type and quantity ordered by quantity descending, and displays the results in an HTML table. It then closes the database connection.

Uploaded by

anup_it2010
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

<?

php 

// create connection 
$connection = mysql_connect("servername","username","password"); 

// test connection 
if (!$connection) { 
echo "Couldn't make a connection!"; 
exit; 

// select database 
$db = mysql_select_db("myDB", $connection); 

// test selection 
if (!$db) { 
echo "Couldn't select database!"; 
exit; 

// create SQL statement 


$sql = "SELECT COFFEE_NAME, ROAST_TYPE, QUANTITY 
FROM COFFEE_INVENTORY 
ORDER BY QUANTITY DESC"; 

// execute SQL query and get result 


$sql_result = mysql_query($sql,$connection); 

// start results formatting 


echo "<TABLE BORDER=1>"; 
echo "<TR><TH>Coffee Name&lt;/TH><TH>Roast Type&lt;/TH><TH>Quantity&lt;/TH>"; 

// format results by row 


while ($row = mysql_fetch_array($sql_result)) { 
$coffee_name = $row["COFFEE_NAME"]; 
$roast_type = $row["ROAST_TYPE"]; 
$quantity = $row["QUANTITY"]; 
echo "<TR><TD>$coffee_name&lt;/TD><TD>$roast_type&lt;/TD><TD>$quantity&lt;/TD>&lt;/TR>
"; 

echo "&lt;/TABLE>"; 

// free resources and close connection 


mysql_free_result($sql_result); 
mysql_close($connection); 
?>

You might also like