[英]JSON PHP data returning null
I am trying to fetch a PHP script using AJAX and return the values as JSON. 我正在尝试使用AJAX提取PHP脚本并将值作为JSON返回。 For some reason, my script fails and I am trying to figure out the problem. 由于某种原因,我的脚本失败,并且我试图找出问题所在。 When I enter a value from the database into the address bar, like: 当我从数据库向地址栏中输入一个值时,例如:
www.someaddress/post.php?kinaseEntry=aValue
I get a JSON output like so: 我得到这样的JSON输出:
{"kinaseSKU":null,"url":null,"molecularWeight":null,"tracerSKU":null,"antiSKU1":"antiSKU1","antiSKU2":"antiSKU2","bufferSKU":"bufferSKU","tracerConc":null,"assayConc":null}
My PHP file looks like so: 我的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;
?>
Am I going about this the wrong way? 我会以错误的方式处理吗? Or have I done this wrong? 还是我做错了?
EDIT: Here is my jQuery/Ajax that calls the PHP script: 编辑:这是我的jQuery / Ajax,它调用PHP脚本:
$(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;
});
});
The mysql_query
function returns a true value even if 0 rows are returned. 即使返回0行, mysql_query
函数也会返回true值。 It returns false only when there was a database error. 仅当发生数据库错误时,它才返回false。 So i believe it executes a query and there are no results, so there are null
values in the JSON. 因此,我相信它会执行查询并且没有结果,因此JSON中null
值。 Try to use mysql_num_rows: 尝试使用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');
}
}
Your problem appears to be that there wasn't actually a row returned from your query. 您的问题似乎是查询实际上没有返回任何行。 Try: 尝试:
$result = mysql_query($findKinase);
if ($result !== false && mysql_num_rows($result) > 0) {
// your code
}
$result
is false
in this case if there was an error in the query. 如果查询中存在错误,则$result
在这种情况下为false
。 If there was no error, then check to see if there were actually rows returned. 如果没有错误,请检查是否实际返回了行。 If there were more than 0 rows returned, then attempt to extract data. 如果有返回大于0的行, 然后尝试提取数据。
First I am not sure if if (isset($_POST['kinaseEntry']))
will work for what you have shown. 首先,我不确定if (isset($_POST['kinaseEntry']))
是否适用于您所显示的内容。 The URL you have shown is a get request, so if you want access to that variable you will have to use $_GET['kinaseEntry']
. 显示的URL是一个get请求,因此,如果要访问该变量,则必须使用$_GET['kinaseEntry']
。 If you need to do POST change the method attribute in your form to <form method="POST">
that will give you the post variable. 如果需要进行POST,请将表单中的method属性更改为<form method="POST">
,这将为您提供post变量。
Yeah, you're getting back an array of rows, so that doesn't really work as you'd expect it to. 是的,您要返回一个行数组,因此这实际上并没有达到您期望的效果。 This should fix it, but I don't think it is the best approach (but I think it should work anyway). 这应该可以解决它,但是我不认为这是最好的方法(但是我认为它仍然应该起作用)。
$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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.