1
<html>
2
<head><title>SQL Demo</title></head>
3
<body>
4
<h1>SQL Demo</h1>
5
This example is taken from the PHP online documentation at <a href="http://php.n
6
et">http://php.net</a>
7
<p>
8
9
<!-- Make Database connection -->
10
<?php
11
12
import_request_variables("GP");
13
14
// Connecting, selecting database
15
$link = mysql_connect('localhost', 'mheadway', 'password')
16
   or die('Could not connect: ' . mysql_error());
17
echo 'Connected successfully<p>';
18
mysql_select_db('mheadway') or die('Could not select database');
19
20
// do we have to add a new entry
21
if (strlen($last . $first . $phone)>0) {
22
  $iquery = "INSERT INTO addressbook ( last , first , phone ) " .
23
    " VALUES ('". $last . "', '" . $first . "', '" . $phone . "')";
24
  echo "Submit query <pre>\n $iquery \n</pre>\n";
25
  $result = mysql_query($iquery) or die('Insert failed: ' . mysql_error());
26
  //  Free resultset
27
  mysql_free_result($result);
28
}
29
30
31
// Performing SQL query
32
switch($order) {
33
 case 'f':
34
   $query = 'SELECT * FROM addressbook order by first';
35
   break;
36
 case 'p':
37
   $query = 'SELECT * FROM addressbook order by phone';
38
   break;
39
 case 'l':
40
 default:
41
   $query = 'SELECT * FROM addressbook order by last';
42
   break;
43
}
44
echo "<p>Query:</p>\n <pre> $query </pre>\n";
45
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
46
47
// Printing results in HTML
48
echo "<table>\n";
49
echo "<tr><td><a href='?order=l'>Last</a>";
50
echo "<td><a href='?order=f'>First</a>";
51
echo "<td><a href='?order=p'>Phone</a>";
52
while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
53
   echo "\t<tr>\n";
54
   foreach ($line as $col_value) {
55
       echo "\t\t<td>$col_value</td>\n";
56
   }
57
   echo "\t</tr>\n";
58
}
59
echo "</table>\n";
60
61
// Free resultset
62
mysql_free_result($result);
63
64
65
66
// Closing connection
67
mysql_close($link);
68
?>
69
70
<hr>
71
72
<form method=get>
73
Last Name <input type=text length=20 name=last><br>
74
First Name <input type=text length=20 name=first><br>
75
Phone Number <input type=text length=10 name=phone><br>
76
<input type=submit name=submit value="Insert">
77
</form>
78
79
</body>
80
</html>
Powered by PHP Code Converter - © 2001-2003, faebu.ch