返回Undefined而不是Value

时间:2012-01-21 10:17:18

标签: php javascript ajax

我有一个页面应该显示AJAX从数据库中获取的一些数据,但它会显示错误消息。

显示数据的页面:

<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>

我收到了这个错误:

  

-undefined警告:mysql_fetch_array():提供的参数不是第11行/home/mastertr/public_html/master/ticker/getinfo.php中的有效MySQL结果资源

3 个答案:

答案 0 :(得分:2)

因为您没有运行$sql1查询

设置:

$sql1="Select OpenPrice, HighPrice, LowPrice from nsepricequotes_latest 
       WHERE Symbol like '". mysql_real_escape_string($q)."' "; 
$result_1 = mysql_query( $sql1 );

然后开始:

while( $row = mysql_fetch_array($result_1) )

而不是:

while( $row = mysql_fetch_array($sql1) )

答案 1 :(得分:1)

您忘记了mysql_query()

$sql1="Select OpenPrice,HighPrice,LowPrice from nsepricequotes_latest WHERE Symbol like '".$q."' "; 
while($row1= mysql_fetch_array($sql1))

应该是

$sql1="Select OpenPrice,HighPrice,LowPrice from nsepricequotes_latest WHERE Symbol like '".$q."' "; 
$res = mysql_query($sql1);
while($row1= mysql_fetch_array($res))

答案 2 :(得分:0)

在鼠标悬停中有以下内容:

showUser()

请注意,undefined和“”不一样。

if (str=="")
  {
document.getElementById("txtHint").innerHTML="";
  return;
  } 

上述方法不起作用,因为str未定义且不是空字符串。 要么这样做:

showUser("")

或者这样做:

if (str == undefined)
相关问题