Hindi Songs

Thursday, July 9, 2009

Example explained - The PHP Page

The PHP page called by the JavaScript, is called "getuser.php".

The PHP script runs an SQL query against a MySQL database, and returns the result as HTML:

< ?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "


<>
<>Firstname< /th >
<>Lastname< /th >
<>Age
<>Hometown< /th >
<>Job< /th >
< /tr >";

while($row = mysql_fetch_array($result))
{
echo "<>";
echo "<>" . $row['FirstName'] . "< / td >";
echo "<>" . $row['LastName'] . "< / td >";
echo "<>" . $row['Age'] . "< / td >";
echo "<>" . $row['Hometown'] . "< / td >";
echo "<>" . $row['Job'] . "< / td >";
echo "< / tr >";
}
echo "< / table >";

mysql_close($con);
? >

When the query is sent from the JavaScript to the PHP page, the following happens:

1. PHP opens a connection to a MySQL server

2. The correct person is found

3. An HTML table is created, and filled with data, and sent back to the "txtHint" placeholder