0

I am developing a webservice using php and MSSQL.I could connect with db and retriev values.But when I use json,It will return no values.

The ajax code used for web service is

    $.ajax({
        url: 'http://209.249.81.138/dev/logintalk.php',
        type: 'POST',
        data: "username=" + $("#username").val()+"&password=" + $("#password").val(),
        dataType: 'json',
        success: function(data){
            alert(data);
        },
        error: function(){
            alert("failure...");
        }
       });
}

and the php code used is

<?php

    $server = "server";
    $username = "username";

    $password = "secret";
    $database = "db";

    $con = mssql_connect($server, $username, $password) or die("Couldn't connect to SQL Server on $Server");

    $users = $_POST['username'];
    $passwd = $_POST['password'];

    $selected = mssql_select_db($database, $con) or die("Couldn't open database $database");

    mssql_query('SET CHARACTER SET utf8'); 
    $query = "SELECT * FROM zNewUsers WHERE memberid = '$users' AND userid = '$passwd'";
    $result = mssql_query($query) or die ("Unable to verify user because ");
    $row = mssql_fetch_array($result);
    $count = mssql_num_rows($result);

    if($count == 0){
        $data=array(0=>"Incorrect values...");
        $value[]=$data[0];
        echo json_encode($value);
    }
    else{
    $val[]=$row['SessionID'];
    echo json_encode($val);
    }  
    mssql_close($con);

?>
Balu
  • 607
  • 3
  • 11
  • 24
  • 1
    When you open the PHP file in your browser, do you see the JSON output? Also, instead of using `alert()` for javascript debugging, use [console.log](http://stackoverflow.com/questions/4743730/javascript-what-is-console-log-and-how-do-i-use-it). – Dzhuneyt Sep 27 '12 at 12:30
  • 1
    Also: use the developer tools of your browser to inspect what data is returned. Maybe the script returns nothing because of a syntax error. – vstm Sep 27 '12 at 12:38
  • 2
    @Balu: You need to change your DB's root password **right now**. – ThiefMaster Sep 27 '12 at 23:55

1 Answers1

0

Few tips beyond question:

  1. success: function(data){
        console.log(data);
        //This is better for debugging if you use firebug, if you don't do it.
    },
    
  2. if($count == 0){
        echo json_encode(array(0=>"Incorrect values..."));
        //Why create new variables? Pass what you want directly to json_encode()
    }
    else{
        echo json_encode($row['SessionID']);
    }
    

Back to question:

var_dump() data you want to encode before encoding and make sure it's OK, then check what you get in browser by console.log(data) and share this information with us so we could help better.

S3Mi
  • 491
  • 4
  • 10