In my project, I use ajax to fetch data from DB. And I test the data content, I choose alert(valData) in success function. But unlucky, nothing return from ajax. I tested
select contact from IDC WHERE id=5;
It works fine in mysql cmd line.
Here is my js code:
var stNum = 5;
$.ajax({
dataType:'json',
type:"POST",
url:"get_ajax_csc.php",
data:{stNum:stNum},
success:function(data)
{
var valData = data;
alert(valData);
}
});
Here is get_ajax_csc.php code:
<?php
if(isset($_POST['stNum']))
{
include("DB.php");
$q=$_POST["stNum"];
$sql="select contact from IDC WHERE id='".$q."';";
$sel = $conn->query($sql);
$arr = $sel->fetch(PDO::FETCH_ASSOC);
echo $arr['contact'];
}
if(isset($_POST['htmlCnt']))
{
include("DB.php");
$htcnt=stripslashes(".$_POST['htmlCnt'].");
........
}
?>
Here is DB.php code:
<?php
session_start();
$pwd=$_SESSION['password'];
$user=$_SESSION['user'];
try
{
$conn = new PDO('mysql:host=x.x.x.x;port=3306;dbname=hpc',$user,$pwd);
}
catch (PDOException $e)
{
echo "account or pwd wrong <meta http-equiv='refresh' content='1;url=index.html'>";
exit;
}
$conn->setAttribute(PDO::ATTR_ORACLE_NULLS, true);
?>
It seems nothing wrong in my code, but I cann't fetch data from DB
I have no idea about the error, who can help me ?