Ïã¸ÛÁùºÏ²Ê

XClose

Information Services Division

Home
Menu

Calling MySQL from PHP

Web pages on the Ïã¸ÛÁùºÏ²Ê Web server which use PHP can access databases on the MySQL server. Full details of the PHP MySQL API (Application Program Interface) can be found in the on-line .

Pages using PHP/MySQL can also be created using Dreamweaver MX.

A page such as the following example should be saved in a file with a .php extension. Note that PHP pages only work in the main , and not in users' personal html.pub web space. This example displays the names and ages in table people in database ucabwww, where the person's age is over 18.

<html>
<head>
<title>PHP/MySQL example</title>
</head>
<body bgcolor="white">
<table border="1">
<tr><th>Name</th><th>Age</th></tr>
<?php
$host="mysql-server.ucl.ac.uk";
$user="ucabwww";
$password="secret";
# Connect to MySQL server
$conn = mysql_connect($host,$user,$password)
   or die(mysql_error());
# Select the database
mysql_select_db("ucabwww", $conn)
   or die(mysql_error());
# Send SQL query
$sql = "SELECT * FROM people WHERE age > 18 ORDER BY name";
$result = mysql_query($sql)
   or die(mysql_error());
# Fetch table rows, one by one, and display as HTML table rows
while ($row = mysql_fetch_assoc($result)) {
   echo "<tr><td>", $row['Name'], "</td><td>", $row['Age'],
      "</td></tr>\n";
}
?>
</table>

</body>

</html>