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);
?>
Few tips beyond question:
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.