Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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);
share|improve this question
3  
` is not a valid character to start a variable in JavaScript or PHP with –  tim Sep 5 '13 at 16:23
    
Danger: You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. –  Quentin Sep 5 '13 at 16:29

1 Answer 1

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.