views:

94

answers:

2

Hi
All i need is a simple explanation on how does this function work
I also attached a piece of php which I think is the one that retrieves the data from the database. Please correct me if I'm wrong

Cheers.

   function loadDatabaseRecords () 
{
// Mozilla/Safari
if (window.XMLHttpRequest) 
{
    xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) 
{
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}

alert ("To Server (Load Records):\n\najax-open-DB.php");

xmlHttpReq.open('GET', "ajax-open-DB.php", true);
xmlHttpReq.onreadystatechange = loadDatabaseRecordsCallback; 
xmlHttpReq.send(null);
}





<?php
  $link = mysql_connect ("ipaddress", "localhost", "password");
  mysql_select_db ("database1");

  $query = "SELECT * from addressbook";
  $result = mysql_query ($query);

  print "<table>";
  print "<tr>";
  print "<th>Firstname</th><th>Lastname</th><th>Address</th><th>Telephone</th>";
  print "</tr>";
  for ($i = 0; $i < mysql_num_rows ($result); $i ++)
  {        
    $row = mysql_fetch_object ($result);
    print "<tr>";
    print "<td>$row->firstname</td>";
    print "<td>$row->lastname</td>";
    print "<td>$row->address</td>";
    print "<td>$row->telephone</td>";
    print "</tr>";
  }
  print "</table>";
  mysql_close ($link);
?>
+3  A: 

mysql_connect connects to MySQL using the hostname (ipaddress), username (localhost) and password (password). select_db then chooses the database (database1).

mysql_query queries the database for all records (select *) in a certain table (addressbook), through the connection you just made. Generally, people also reference the connection, as in mysql_query ($query, $link)

fetch_object fetches the next row from that query, one at a time, and php formats the results with td/tr, etc.

MJB
Not exactly what I was looking for, but maybe I didn't make the question well. Better than the "That isn't a question" guy.Thanks i appreciate it :)
Audel
yeah, I only addressed the PHP and MySQL part -- I was not sure what the question was exactly.
MJB
+2  A: 

With Ajax you can update only part of your view. The information from you MySQL database on the server side is "mixed" with the current page on the client side that the user is viewing. If you have complex structures and presentation details, Ajax can save you time by skipping redundant information the client knows already. You might be interested to take a look into the JQuery load API which makes Ajax easier to use.

poseid