Fetching Data From MySQL With Ajax In PHP – Info PHP
Introduction
This article explains fetching data from MySQL with Ajax in PHP. To do that you will first create a table and store some data in it for retrieving with Ajax and transform the array data from MySQL to XML using a DOM element. Ajax will read the data from the MySQL server form of XML data using PHP code. First of all You will create a client-side file use of HTML with Ajax and then create a connection with your database in MySQL then create the PHP code.

Example
This is the “index.html” file.
“Content-Type” content=“text/html; charset=utf-8” />
“get_ready()”>
Your records:
And this is the “index.php” code and this file will create a XML document.
header(‘Content-type: text/xml’);
mysql_connect(‘localhost’,‘root’,”);
mysql_select_db(‘demo’);
$query = “Select * from `data`”;
$result = mysql_query($query) or die(mysql_error());
$dom_elements = new DOMDocument();
$ddda = $dom_elements->createElement(‘ddda’);
$dom_elements->appendChild($ddda);
while($r = mysql_fetch_array($result)){
$id = $dom_elements->createElement(‘id’);
$idText = $dom_elements->createTextNode($r[‘id’]);
$id->appendChild($idText);
$books = $dom_elements->createElement(‘books’);
$booksText = $dom_elements->createTextNode($r[‘books’]);
$books->appendChild($booksText);
$written_by = $dom_elements->createElement(‘written_by’);
$written_byText = $dom_elements->createTextNode($r[‘written_by’]);
$written_by->appendChild($written_byText);
$book = $dom_elements->createElement(‘book’);
$book->appendChild($id);
$book->appendChild($books);
$book->appendChild($written_by);
$ddda->appendChild($book);
}
$string = $dom_elements->saveXML();
echo $string;
?>
Output

Article Prepared by Ollala Corp
