0

Possible Duplicate:
JSON encode MySQL results

Here in my code data is coming directly from SQL to my page. but i want data should be accessed through JSON, in between MYSQL & PHP file.. so what i have to change in following code???

PHP Code:


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

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

mysql_select_db("test", $con);

$sql="SELECT * FROM artist_details WHERE name = '".$q."'";

$result = mysql_query($sql) ;

$num=mysql_numrows($result);
if($num!=0)
{
echo "<table border='5'>
<tr>
<th>n</th>
<th>a</th>
<th>s</th>
<th>p</th>
</tr>";


while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['n'] . "</td>";
echo "<td>" . $row['a'] . "</td>";
echo "<td>" . $row['s'] . "</td>";
echo "<td>" . $row['p'] . "</td>";
echo "</tr>";
}


echo "</table>";
}
else
die('record not found');

mysql_close($con);
?>    

HTML CODE:


     <html>
<head>
<script type="text/javascript">
function showUser(str)
{

if (str=="")
{
document.getElementById("txtHint").inn…
return;
} 
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("txtHint").inn…
}
}
xmlhttp.open("GET","http://localhost/n…
xmlhttp.send();
}
</script>
</head>
<body bgcolor="ffcccc">
<b>
<form align='center'>
Select a person:
<select name="users" onchange="showUser(this.value)">
<option value="i">i</option>
<option value="d">d</option>
<option value="r">r</option>
<option value="a">a</option>
<option value="a">a</option>
<option value="a">a</option>
</select>
</form>
<div id="txtHint" align='center'><font color="3333ff">Person info will be listed here.<font></div>
</body>
</html>

.

Community
  • 1
  • 1
  • 1
    You just add [`json_encode()`](http://php.net/json_encode) on the DB results instead of outputting HTML. – mario Oct 10 '12 at 12:52
  • 3
    For the love of god, stop using mysql_* functions. Switch to mysqli or PDO – asprin Oct 10 '12 at 12:55
  • 1
    what @asprin is trying to say is that the `mysql_xx()` functions are considered **obsolete and insecure**, and the PHP developers are in the process of deprecating them. They are therefore not recommended for use. The PHP manual has details of this, and recommends switching to the `mysqli_xxx()` functions or the PDO library. – SDC Oct 10 '12 at 12:58

2 Answers2

0

On PHP side you need to use json_encode function which convert array to json string like that.

$data = array();
while($row = mysql_fetch_array($result))
{
    $data[] = $row;
}

echo json_encode($data); 

Then you can change your code in javascript to get json object from response.

var jsonResponse = JSON.parse(req.responseText);

However you can use some good JS library like jQuery for Ajax operations for easy compatibility for different browsers. Also it is better to use mysqli or PDO instead of mysql extension like others say.

kuldeep.kamboj
  • 2,566
  • 3
  • 26
  • 63
0

It is better to use jQuery Ajax. it does all foundations and checks for you. you start making ajax call. go to jQuery and read the documentation: http://api.jquery.com/jQuery.get/

examples:

//PHP Code data.php
$result = array();
while($row = mysql_fetch_array($result))
{
   $result[] = $row;
}


return json_encode($result);


//Jquery Code
<script>
$.get("data.php", function(result){
    var rows = JSON.parse(result);

    //do the loop and inject into html table
    }); 
</script>
Nwafor
  • 184
  • 6