1

I want to get searched data from mysql database using JSON and show in my php page. I was write this code but it not retrieve any data.please help me

Client page

$(function () {
    var roll = document.getElementById("roll").value;
    $.ajax({
        type: "POST",
        url: 'api.php',
        data: "roll=" + roll,
        dataType: 'json',
        success: function (data) {
            var id = data[0];
            var vname = data[1];`$` ('#output').html("id: " + id + " name: " + vname);
        }
    });
});

api.php

$host = "localhost";
$user = "root";
$pass = "";
$databaseName = "ajax";
$tableName = "stud";
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
  if(isset($_POST['roll'])){
      $data = $_POST['roll'];
        $result = mysql_query("SELECT * FROM $tableName WHERE roll = '".$data."'");
        $array = mysql_fetch_row($result);        
}
echo json_encode($array);
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
user2006506
  • 119
  • 1
  • 2
  • 13
  • 3
    **`** is not a valid character to start a variable in JavaScript or PHP with – tim Sep 05 '13 at 16:23
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Sep 05 '13 at 16:29

1 Answers1

0

log this value before sending,

var roll = document.getElementById("roll").value;
console.log(roll);

Use object to send the params in ajax call like this data: {'roll':roll} for best practice

use firebug to check if the value 'roll' passed properly

In your php dump the post variable print_r($_POST) and check the firebug response console whether you got what you sent.

If you got it in $_POST then probably you have some issue with sql connection/query

rAjA
  • 899
  • 8
  • 13