-2

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:

-undefined Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/mastertr/public_html/master/ticker/getinfo.php on line 11

outis
  • 75,655
  • 22
  • 151
  • 221
Manoj
  • 9
  • 1
  • 9
  • Please read the docs. You're using `mysql_fetch_array` incorrectly, and the docs have an example of what you should be doing. – Mat Jan 21 '12 at 10:21
  • sorry but the last one was not editing can you please explain am not getting – Manoj Jan 21 '12 at 10:51
  • @ĦāwťlįcįOuxRâwkstärßøį Exactly which part is throwing the error? – Kemal Fadillah Jan 21 '12 at 11:16
  • The mysql extension is outdated and on its way to deprecation. New code should use mysqli or PDO, both of which have important advantages, such as support for prepared statements. – outis Jan 21 '12 at 18:34
  • Don't use [`SELECT *`](http://stackoverflow.com/questions/321299/) unless you're writing a DB administration program; select only the columns you need. – outis Jan 21 '12 at 18:35
  • Instead of executing multiple queries in a loop using data retrieved from a previous query, use a [join](http://www.sql-tutorial.com/sql-join-sql-tutorial/). – outis Jan 21 '12 at 18:38
  • 1
    [Sample code](http://sscce.org/) should be complete, concise and representative. What's posted has too much extraneous code. – outis Jan 21 '12 at 18:38

3 Answers3

2

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) )
jogesh_pi
  • 9,762
  • 4
  • 37
  • 65
  • its running here fetching data from database $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']; } – Manoj Jan 21 '12 at 10:24
  • i tried now am getting Parse error: syntax error, unexpected T_WHILE in /home/mastertr/public_html/master/ticker/getinfo.php on line 12 – Manoj Jan 21 '12 at 10:28
  • parse error generated when you missing something like `}` or semicolumn etc. – jogesh_pi Jan 21 '12 at 10:33
  • its showing parse error in line no 12 line no 12 is while( $row = mysql_fetch_array($result_1) ) – Manoj Jan 21 '12 at 10:34
  • first thing edit the given code according to your requirenment like your getting the fetched result with `$row1` then you should have to use `$row1` instead of `$row`, and if you again get the error then plz re-edit your code with changes – jogesh_pi Jan 21 '12 at 10:40
  • edited code here http://stackoverflow.com/questions/8952387/returns-undefined-instead-of-value-ajax-javascript please check – Manoj Jan 21 '12 at 10:57
1

You forgot to run mysql_query()

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

should be something like

$sql1="Select OpenPrice,HighPrice,LowPrice from nsepricequotes_latest WHERE Symbol like '".$q."' "; 
$res = mysql_query($sql1);
while($row1= mysql_fetch_array($res))
Kemal Fadillah
  • 9,760
  • 3
  • 45
  • 63
  • look preciously mysql_query() has used – Dau Jan 21 '12 at 10:25
  • @ĦāwťlįcįOuxRâwkstärßøį You query probably just failing for some reason. What's the value of `$q`? And are you sure you don't need to escape it? – Kemal Fadillah Jan 21 '12 at 11:06
  • the value of $Q is ACC the edited code is here http://stackoverflow.com/questions/8952387/returns-undefined-instead-of-value-ajax-javascript – Manoj Jan 21 '12 at 11:08
0

In your mouseover there's the following:

showUser()

Note that undefined and "" is not the same.

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

The above doesn't work because str is undefined and not an empty string. Either do this:

showUser("")

Or do this:

if (str == undefined)
Rick Kuipers
  • 6,616
  • 2
  • 17
  • 37