CREATE TABLE items (
itemID int(11) NOT NULL auto_increment,
itemName varchar(255) NOT NULL default '',
itemPrice float NOT NULL default '0',
PRIMARY KEY (itemID) ) TYPE=MyISAM;
INSERT INTO items VALUES (1, 'Paperweight', '3.99');
INSERT INTO items VALUES (2, 'Key ring
', '2.99');
INSERT INTO items VALUES (3, 'Commemorative plate', '14.99');
INSERT INTO items VALUES (4, 'Pencils (set of 4)', '1.99');
INSERT INTO items VALUES (5, 'Coasters (set of 3)', '4.99');
<html>
<head></head>
<body>
<?php
// open connection to MySQL server
$connection = mysql_connect('localhost', 'guest', 'pass') ?
or die ('Unable to connect!');
// select database for use
mysql_select_db('db2') or die ('Unable to select database!');
// create and execute query
$query = 'SELECT * FROM items';
$result = mysql_query($query) ?
or die ('Error in query: $query. ' . mysql_error());
// check if records were returned
if (mysql_num_rows($result) > 0)
{
// print HTML table
echo '<table width=100% cellpadding=10 cellspacing=0 border=1>';
echo
'<tr><td><b>ID</b></td><td><b>Name</b></td><td><b>Price</b></td></tr>';
// iterate over record set
// print each field
while($row = mysql_fetch_row($result))
{
echo '<tr>';
echo '<td>' . $row[0] . '</td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '</tr>';
}
echo '</table>';
}
else
{
// print error message
echo 'No rows found!';
}
// once processing is complete
// free result set
mysql_free_result($result);
// close connection to MySQL server
mysql_close($connection);
?>
</body>
</html>
Copyright GeekInterview.com
Latest Code Samples
Popular Code Samples
Related Code Samples
Comments