本文介绍了返回undefined,而不是价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网页,应该显示由AJAX从数据库中获取的一些数据,而是它会显示错误消息。
这显示了数据的页面:
< HTML>
< HEAD>
<风格类型=文本/ CSS>
一个 {
文字修饰:无;
}
< /风格>
<脚本类型=文/ JavaScript的>
功能showUser(STR){
如果(STR ==){
的document.getElementById(txtHint)的innerHTML =。
返回;
}
如果(window.XMLHtt prequest){
// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
XMLHTTP =新XMLHtt prequest();
} 其他 {
// code对IE6,IE5
XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
}
xmlhttp.onreadystatechange =功能(){
如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
的document.getElementById(txtHint)的innerHTML = xmlhttp.responseText。
}
}
xmlhttp.open(GET,getinfo.php Q =?+ STR,真正的);
xmlhttp.send();
}
< / SCRIPT>
< /头>
<身体GT;
< PHP
的mysql_connect(localhost,则用户名,密码);
mysql_select_db(数据库名称);
// $ niftystocks =阵列();
$平方=SELECT TPNT code从`niftystock`;
$ R =请求mysql_query($平方米);
$ i = 0;
而($ RO = mysql_fetch_array($ R)){
// array_push($ niftystocks,$行['TPNT code']);
$ tpnt = $ RO ['TPNT code'];
$ SQL =SELECT * FROM`nsepricequotes_latest`那里TickerPlant code ='$ tpnt';
$ RS =的mysql_query($ SQL);
$行= mysql_fetch_array($ RS);
如果($ I == 0)
回声 - 。 $行['日期时间'。 - ;
$ SY = $行['符号'];
回声&所述;跨度的id ='txtHint'>&所述; /跨度>&所述; A HREF =#>&所述;跨越的onmouseover ='showUser()'> $ SY&所述; /跨度计算值:。 $行['LastTradedPrice']。 ($行['PercentChange']。)&所述; / A>中;
如果($行['PercentChange']≥= 0)
回声< IMG SRC =HTTP://mastertrade.in/master/ticker/images/arrow-up.gif边框='0'> |;
其他
回声< IMG SRC =HTTP://mastertrade.in/master/ticker/images/arrow-down.gif边框='0'> |;
$ I ++;
}
?>
getinfo.php:
< PHP
$ Q = $ _ GET ['Q'];
回声$ Q;
$ CON =的mysql_connect(本地主机,用户名,密码);
如果(!$ CON){
死亡(无法连接:mysql_error());
}
mysql_select_db(数据库名称,$ CON);
$ SQL1 =从nsepricequotes_latest其中symbol喜欢选择OpenPrice,HighPrice,LowPrice,$ Q';
而($ ROW1 = mysql_fetch_array($ SQL1)){
$ openprice = $ ROW1 ['OpenPrice'];
$ highprice = $ ROW1 ['HighPrice'];
$ lowprice = $ ROW1 ['LowPrice'];
$ tpnt = $ ROW1 ['TickerPlant code'];
}
$ SQL =SELECT * FROM 52wkhighlow WHERE NFT code ='$ tpnt';
$结果= mysql_query($ SQL);
而($行= mysql_fetch_array($结果)){
$ wkhigh = $行['BSE52WkHighVal'];
$ wklow = $行['BSE52wlLowval'];
}
?>
< HTML>
<身体GT;
<表>
&其中; TR>
< TD>开放式价格与LT; / TD>< TD>< PHP的echo $ openprice; ?>< / TD>
< TD>高价格< / TD>< TD>< PHP的echo $ highprice; ?>< / TD>
< TD>低价格< / TD>< TD>< PHP的echo $ lowprice;?>< / TD>
< TD> 52周新高< / TD>< TD>< PHP的echo $ wkhigh;?>< / TD>
< TD> 52周最低:LT; / TD>< TD>< PHP的echo $ wklow;?>< / TD>
< / TR>
< /表>
< /身体GT;
< / HTML>
我得到这个错误:
解决方案
因为你没有运行 $ SQL1
查询
设置这样的:
$ SQL1 =选择OpenPrice,HighPrice,LowPrice从nsepricequotes_latest
WHERE符号,如'mysql_real_escape_string($ Q)。;
$ result_1 =请求mysql_query($ SQL1);
再启动:
而($行= mysql_fetch_array($ result_1))
来代替:
而($行= mysql_fetch_array($ SQL1))
I have a page that is supposed to display some data fetched by AJAX from a database, but instead it displays an error message.
The page that shows the data:
<html>
<head>
<style type="text/css">
a {
text-decoration:none;
}
</style>
<script type="text/javascript">
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getinfo.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<?php
mysql_connect("localhost","username","password");
mysql_select_db("databasename");
//$niftystocks=array();
$sq="SELECT TPNTCode FROM `niftystock`";
$r=mysql_query($sq);
$i=0;
while ($ro=mysql_fetch_array($r)) {
//array_push($niftystocks,$row['TPNTCode']);
$tpnt=$ro['TPNTCode'];
$sql="SELECT * FROM `nsepricequotes_latest` where TickerPlantCode = '$tpnt' ";
$rs=mysql_query($sql);
$row=mysql_fetch_array($rs);
if ($i == 0)
echo "--" . $row['DateTime'] . "--";
$sy=$row['Symbol'];
echo "<span id='txtHint'></span><a href='#'><span onmouseover='showUser()'>$sy</span>: " . $row['LastTradedPrice'] . " (" . $row['PercentChange'] . ")</a>";
if($row['PercentChange'] >= 0)
echo " <img src='http://mastertrade.in/master/ticker/images/arrow-up.gif' border='0' > | ";
else
echo " <img src='http://mastertrade.in/master/ticker/images/arrow-down.gif' border='0' > | ";
$i++;
}
?>
getinfo.php:
<?php
$q=$_GET['q'];
echo $q;
$con = mysql_connect('localhost', 'username', 'password');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databasename", $con);
$sql1="Select OpenPrice,HighPrice,LowPrice from nsepricequotes_latest WHERE Symbol like '".$q."' ";
while($row1= mysql_fetch_array($sql1)) {
$openprice=$row1['OpenPrice'];
$highprice=$row1['HighPrice'];
$lowprice=$row1['LowPrice'];
$tpnt=$row1['TickerPlantCode'];
}
$sql="SELECT * FROM 52wkhighlow WHERE nFTCode = '$tpnt'";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$wkhigh=$row['BSE52WkHighVal'];
$wklow=$row['BSE52wlLowval'];
}
?>
<html>
<body>
<table>
<tr>
<td>Open Price</td><td><?php echo $openprice; ?></td>
<td>High Price</td><td><?php echo $highprice; ?></td>
<td>Low Price</td><td><?php echo $lowprice;?></td>
<td>52 Week High</td><td><?php echo $wkhigh;?></td>
<td>52 Week Low</td><td><?php echo $wklow;?></td>
</tr>
</table>
</body>
</html>
I'm getting this error:
解决方案
because you didn't run your $sql1
query
set this :
$sql1="Select OpenPrice, HighPrice, LowPrice from nsepricequotes_latest
WHERE Symbol like '". mysql_real_escape_string($q)."' ";
$result_1 = mysql_query( $sql1 );
then start :
while( $row = mysql_fetch_array($result_1) )
instead of :
while( $row = mysql_fetch_array($sql1) )
这篇关于返回undefined,而不是价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!