我正在尝试使用AJAX获取PHP脚本并将值作为JSON返回。出于某种原因,我的脚本失败了,我试图找出问题所在。当我从数据库中输入一个值到地址栏时,如:
www.someaddress/post.php?kinaseEntry=aValue
我得到一个JSON输出:
{"kinaseSKU":null,"url":null,"molecularWeight":null,"tracerSKU":null,"antiSKU1":"antiSKU1","antiSKU2":"antiSKU2","bufferSKU":"bufferSKU","tracerConc":null,"assayConc":null}
我的PHP文件如下:
<?php
//Include connection to database
require_once 'connect.php';
$kinase = mysql_real_escape_string ($_POST["kinaseEntry"]);
mysql_query('SET CHARACTER SET utf8');
$findKinase = "SELECT * FROM kbaData where cleanSKU = '" .$kinase. "' ";
if ($result = mysql_query($findKinase)) {
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$kinaseSKU = $row['cleanSKU'];
$url = $row['url'];
$molecularWeight = $row['molecularWeight'];
$tracerSKU = $row['tracerSKU'];
$antiSKU1 = $row['antiSKU1'];
$antiSKU2 = $row['antiSKU2'];
$bufferSKU = $row['bufferSKU'];
$tracerConc = $row['tracerConc'];
$assayConc = $row['assayConc'];
/* JSON ROW */
$json = array ("kinaseSKU" => $kinaseSKU, "url" => $url, "molecularWeight" => $molecularWeight, "tracerSKU" => $tracerSKU, "antiSKU1" => $antiSKU1, "antiSKU2" => $antiSKU2, "bufferSKU" => $bufferSKU, "tracerConc" => $tracerConc, "assayConc" => $assayConc );
} else {
/* CATCH ANY ERRORS */
$json = array('error' => 'Mysql Query Error');
}
/* SEND AS JSON */
header("Content-Type: application/json", true);
/* RETURN JSON */
echo json_encode($json);
/* STOP SCRIPT */
exit;
?>
我是以错误的方式来做这件事的吗?或者我做错了吗?
编辑:这是我调用PHP脚本的jQuery / Ajax:$(document).ready(function() {
$('#kinaseEntry').change(function () {
var kinaseEntry = $('#kinaseEntry').val();
var dataString = 'kinaseEntry' + kinaseEntry;
$('#waiting').show(500);
$('#message').hide(0);
alert(kinaseEntry);
//Fetch list from database
$.ajax({
type : "POST",
url : "post.php",
datatype: "json",
data: dataString,
success : function(datas) {
alert("datas" + datas);
},
error : function(error) {
alert("Oops, there was an error!");
}
});
return false;
});
});
答案 0 :(得分:1)
即使返回0行,mysql_query
函数也会返回true值。仅在出现数据库错误时才返回false。所以我相信它执行一个查询并且没有结果,因此JSON中有null
个值。尝试使用mysql_num_rows:
$findKinase = "SELECT * FROM kbaData where cleanSKU = '" .$kinase. "' ";
if ($result = mysql_query($findKinase)) {
$json = array('error' => 'Mysql Query Error');
}else{
$num_rows = mysql_num_rows($result);
if($num_rows > 0){
// Do sth with the results
}else{
$json = array('error' => 'No results');
}
}
答案 1 :(得分:0)
您的问题似乎是您的查询实际上没有返回任何行。尝试:
$result = mysql_query($findKinase);
if ($result !== false && mysql_num_rows($result) > 0) {
// your code
}
如果查询中存在错误,则 $result
在此情况下为false
。如果没有错误,则检查是否确实返回了行。如果 超过0行返回,则尝试提取数据。
答案 2 :(得分:0)
首先,我不确定if (isset($_POST['kinaseEntry']))
是否适用于您所展示的内容。您显示的网址是获取请求,因此如果您想要访问该变量,则必须使用$_GET['kinaseEntry']
。如果您需要进行POST,请将表单中的method属性更改为<form method="POST">
,以便为您提供post变量。
答案 3 :(得分:0)
是的,你回来了一系列行,所以这并不像你期望的那样真正起作用。这应该解决它,但我不认为这是最好的方法(但我认为它应该工作)。
$kinase = mysql_real_escape_string ($_POST["kinaseEntry"]);
mysql_query('SET CHARACTER SET utf8');
$findKinase = "SELECT * FROM kbaData where cleanSKU = '" .$kinase. "' , LIMIT 0,1";
if ($result = mysql_query($findKinase)) {
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$kinaseSKU = $row[0]['cleanSKU'];
$url = $row[0]['url'];
$molecularWeight = $row[0]['molecularWeight'];
$tracerSKU = $row[0]['tracerSKU'];
$antiSKU1 = $row[0]['antiSKU1'];
$antiSKU2 = $row[0]['antiSKU2'];
$bufferSKU = $row[0]['bufferSKU'];
$tracerConc = $row[0]['tracerConc'];
$assayConc = $row[0]['assayConc'];
/* JSON ROW */
$json = array ("kinaseSKU" => $kinaseSKU, "url" => $url, "molecularWeight" => $molecularWeight, "tracerSKU" => $tracerSKU, "antiSKU1" => $antiSKU1, "antiSKU2" => $antiSKU2, "bufferSKU" => $bufferSKU, "tracerConc" => $tracerConc, "assayConc" => $assayConc );
} else {
/* CATCH ANY ERRORS */
$json = array('error' => 'Mysql Query Error');
}
/* SEND AS JSON */
header("Content-Type: application/json", true);
/* RETURN JSON */
echo json_encode($json);
/* STOP SCRIPT */
exit;