PHP/SQL Database
Posted: Sun Aug 01, 2004 11:24 am
Bellow is a Section of an assignment i have completed, could anyone suggest a different way of writing the code or if indeed there is any other way of writing the code? I hate Uni, it sucks, but the thigns we do to get money...!!! Seriously, any help would be greatly appreciated....
2. Write a MySQL query that will insert the following address into the
table ”addresses” assuming an active connection to the database ”personal”.
Mickey Mouse, 1 Disney Parade, Disneyland, USA.
3. (3%) Write a script that will connect to the database and table above, ready
for further use; (Assume the database is on the server localhost, and
that the username and password are both simply “test”).
4. (3%) Augment the script to perform a SELECT query to fetch all address
entries ordered by the name column in alphabetical order.
5. (3%) Augment the script to show the fetch entries, with each entry in a table
of its own.
2.-----------------------------------------------------------------------------
INSERT into personal (name, address1, town, country) values (‘Mickey Mouse’, ‘1 Disney Parade’, ‘Disneyland’, ‘USA’);
3.---------------------------------------------------------------------------
<?php
//set variables for database access:
$host = “localhost”;
$user = “test”;
$pass = “test”;
$dbname = “personal”;
$tblname = “addresses”;
$conn = mysql_connect ($host, $user, $pass);
?>
4.-------------------------------------------------------------------------
<?php
//set variables for database access:
$host = “localhost”;
$user = “test”;
$pass = “test”;
$dbname = “personal”;
$tblname = “addresses”;
$conn = mysql_connect ($host, $user, $pass);
$query = “SELECT * FROM $tblname ORDER BY name”;
$result = mysql_db_query($dbname, $query, $conn);
mysql_close ($conn);
?>
5.------------------------------------------------------------------------
<?php
//set variables for database access:
$host = “localhost”;
$user = “test”;
$pass = “test”;
$dbname = “personal”;
$tblname = “addresses”;
$conn = mysql_connect ($host, $user, $pass);
$query = “SELECT * FROM $tblname ORDER BY name”;
$result = mysql_db_query($dbname, $query, $conn);
//loop through records and output into table
while ($table = mysql_fetch_array ($query)){
print (“<table border=1 width =\”75%\” cellspacing=2 cellpadding=2 align=centre> \n”);
print (“<tr> \n”)
print (“<td>$table[name]</td> \n”)
print (“</tr> \n”)
print (“<tr> \n”)
print (“<td>$table[address1]<br>$table[address2]<br>$table[town] $table[postcode]<br>$table[country]</td> \n”)
print (“</tr> \n”)
print (“<tr> \n”)
print (“<td>$table[record_id]</td> \n”)
print (“</tr> \n”)
print (“</table> \n”)
}
mysql_close ($conn);
?>
2. Write a MySQL query that will insert the following address into the
table ”addresses” assuming an active connection to the database ”personal”.
Mickey Mouse, 1 Disney Parade, Disneyland, USA.
3. (3%) Write a script that will connect to the database and table above, ready
for further use; (Assume the database is on the server localhost, and
that the username and password are both simply “test”).
4. (3%) Augment the script to perform a SELECT query to fetch all address
entries ordered by the name column in alphabetical order.
5. (3%) Augment the script to show the fetch entries, with each entry in a table
of its own.
2.-----------------------------------------------------------------------------
INSERT into personal (name, address1, town, country) values (‘Mickey Mouse’, ‘1 Disney Parade’, ‘Disneyland’, ‘USA’);
3.---------------------------------------------------------------------------
<?php
//set variables for database access:
$host = “localhost”;
$user = “test”;
$pass = “test”;
$dbname = “personal”;
$tblname = “addresses”;
$conn = mysql_connect ($host, $user, $pass);
?>
4.-------------------------------------------------------------------------
<?php
//set variables for database access:
$host = “localhost”;
$user = “test”;
$pass = “test”;
$dbname = “personal”;
$tblname = “addresses”;
$conn = mysql_connect ($host, $user, $pass);
$query = “SELECT * FROM $tblname ORDER BY name”;
$result = mysql_db_query($dbname, $query, $conn);
mysql_close ($conn);
?>
5.------------------------------------------------------------------------
<?php
//set variables for database access:
$host = “localhost”;
$user = “test”;
$pass = “test”;
$dbname = “personal”;
$tblname = “addresses”;
$conn = mysql_connect ($host, $user, $pass);
$query = “SELECT * FROM $tblname ORDER BY name”;
$result = mysql_db_query($dbname, $query, $conn);
//loop through records and output into table
while ($table = mysql_fetch_array ($query)){
print (“<table border=1 width =\”75%\” cellspacing=2 cellpadding=2 align=centre> \n”);
print (“<tr> \n”)
print (“<td>$table[name]</td> \n”)
print (“</tr> \n”)
print (“<tr> \n”)
print (“<td>$table[address1]<br>$table[address2]<br>$table[town] $table[postcode]<br>$table[country]</td> \n”)
print (“</tr> \n”)
print (“<tr> \n”)
print (“<td>$table[record_id]</td> \n”)
print (“</tr> \n”)
print (“</table> \n”)
}
mysql_close ($conn);
?>