---
title: "Using AJAX with Database in PHP"  
description: "AJAX can be used for interactive communication with a database.  The following example will demonstrate how a web page can fetch information from a da"  
author: "Anonymous User"  
published: 2011-09-27  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/745/using-ajax-with-database-in-php  
category: "php"  
tags: ["php"]  
reading_time: 2 minutes  

---

# Using AJAX with Database in PHP

AJAX can be used for [interactive](https://answers.mindstick.com/qa/38931/who-has-won-the-2016-fifa-interactive-world-cup-fiwc) [communication](https://www.mindstick.com/articles/126321/5-fails-and-fixes-of-the-office-communication) with a database.\

The following example will demonstrate how a [web page](https://answers.mindstick.com/qa/92898/how-to-create-a-web-page-using-bootstrap-4) can [fetch information](https://answers.mindstick.com/qa/93905/how-to-fetch-information-from-a-normal-file-with-ajax) from a database with AJAX:

##### Example:

The [database table](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table) we use in the example above looks like this:

![Using AJAX with Database in PHP](https://www.mindstick.com/mindstickarticle/aa044953-9f8c-4666-932e-946b2d5bc7ef/images/84435ba4-daf0-4ca5-9380-dd1789f689c0.png)

In this example, I will show you how a web page can fetch information from a database by using, selecting user name from user list.

```
<!DOCTYPE html><html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">         <title>AJAX with Database</title>         <script type="text/javascript">               function getUserDetails(str) {                 if (str== "") {                    document.getElementById("userDetails").innerHTML= "";                     return;                }                 if (window.XMLHttpRequest) {                    xmlHttp=newXMLHttpRequest();                }                xmlHttp.onreadystatechange=function () {                     if (xmlHttp.readyState==4) {                          document.getElementById("userDetails").innerHTML= xmlHttp.responseText;                    }                }                xmlHttp.open("GET", "DatabaseAjax.php?drdnVal="+str, true);                xmlHttp.send();            }     </script>     </head>     <body>                <p style="background-color: gold;  width: 200px">Select User Name :</p>            <form>                <select id ="usrName" onchange="getUserDetails(this.value)" >                      <option value=""> Select User Name  </option>                     <option value="Arun"> Arun  </option>                     <option value="Amit"> Amit  </option>                     <option value="Vineet"> Vineet</option>                     <option value="Akki"> Akki</option>                     <option value="Ray"> Ray</option>                 </select>             </form>         <div id="userDetails">             <p> <b> User Details:</b></p>            </div>     </body></html>
```

\

![Using AJAX with Database in PHP](https://www.mindstick.com/mindstickarticle/aa044953-9f8c-4666-932e-946b2d5bc7ef/images/8c5452a4-da4b-4f85-8fc3-9a0a79aef106.png)

Now [select](https://www.mindstick.com/forum/34066/use-select-operator-in-linq) any [user name](https://answers.mindstick.com/qa/30717/how-to-retrieve-user-name-in-case-of-window-authentication) from user list, when you select any user name then ‘DatabaseAjax.php’ will be called.

##### ‘DatabaseAjax.php’ code:

```
<?php         // Get dropdown value from url         $val =$_GET["drdnVal"];          // create connection with database        $con = mysql_connect("localhost", "root", "root");        // Check connection successfully created or no        if(!$con)        {            // display error message when connection creation failed            die ("<b>Error : ". mysql_error());          }        // select database name         mysql_select_db("tempdatabase",$con);        //select record from table         $qry = "Select * from tempdatabase.tbltemp where username = '".$val."'";        // query result         $qryResult = mysql_query($qry,$con);        //print table structure          echo "<table border='1px solid balck'>             <tr>             <th> Id  </th>             <th> UserName  </th>             <th> EmailId  </th>             <th> Address  </th>             <th> Mobile No  </th>             </tr>                ";          // Display data in table column        while ($rowData = mysql_fetch_array($qryResult))        {            echo "<tr>";            echo "<td>".$rowData['Id']."</td>";            echo "<td>".$rowData['Username']."</td>";            echo "<td>".$rowData['EmailId']."</td>";            echo "<td>".$rowData['Address']."</td>";            echo "<td>".$rowData['MobileNo']."</td>";            echo "</tr>";         }         // close table tag         echo "</table>" ;          // close the connection        mysql_close($con);  ?>
```

##### Output:

![Using AJAX with Database in PHP](https://www.mindstick.com/mindstickarticle/aa044953-9f8c-4666-932e-946b2d5bc7ef/images/90d2aa8c-5496-41c4-9507-fd0c3a7aa16a.png)

Select any one user name user list; here I am selecting ‘Arun’

![Using AJAX with Database in PHP](https://www.mindstick.com/mindstickarticle/aa044953-9f8c-4666-932e-946b2d5bc7ef/images/aee5096c-cbb0-4e11-b4ce-8ae5b041c069.png)

\

---

Original Source: https://www.mindstick.com/articles/745/using-ajax-with-database-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
