Note that there are some explanatory texts on larger screens.

plurals
  1. POAjax page does not update consistently
    primarykey
    data
    text
    <p>I've recently started exploring using AJAX for a small project, and I've had reasonable success though it's not as smooth as I would like.</p> <p>The Basic setup is that an applications named ProphetX which interfaces with Excel to show stock market prices. Prices are updated as they change in Excel. Using VBA I save the data from the spreadsheet into an SQL08 DB every time a price updates. This could sometimes be a few times per second.</p> <p>Using PHP on an Apache server I connect to the SQL DB and load the data into tables, and a javascript function to keep updating the information once every second. I've noticed however that at times the page will simply hang if you already have it up, or load a blank screen if you pull it up, particularly when data is being updated rapidly. So to the meat of my question: Is there something in my code which could be causing this hiccup? I doubt it is congesting the network or server resources as I've been monitoring them and they seem low.</p> <p>Also I used WireShark to monitor network traffic, and when I load up the page and it shows blank in the browser, I can see the HTML being sent from the server to my machine.</p> <p>Any help / coding style criticism is much appreciated, source code below.</p> <p>Index.php:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" src="update.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body onLoad = update()&gt; &lt;font size = +2&gt; &lt;?php echo " &lt;div id = 'marketData'&gt;&lt;/div&gt; "; ?&gt; &lt;/font&gt; &lt;/body&gt;&lt;/html&gt; </code></pre> <p>Update.js:</p> <pre><code>function update() { 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 &amp;&amp; xmlhttp.status==200) { document.getElementById("marketData").innerHTML=xmlhttp.responseText; } } //URL needs a var to be passed via get for code to function in IE, thus Math.Random(). //I am also confused by this requirement. xmlhttp.open("GET","update.php?i="+ Math.random(),true); xmlhttp.send(); var t = setTimeout("update()", 3000); } </code></pre> <p>Update.php:</p> <pre><code>&lt;?php //connect to database error_reporting(0); sqlsrv_configure("WarningsReturnAsErrors", 1); $server = "myServer"; $db = "myDB"; $connectionInfo = array("Database"=&gt;"$db, "UID"=&gt;"user", "PWD"=&gt;"pass"); $conn = sqlsrv_connect($server, $connectionInfo); if($conn) { echo "&lt;font size =-1 color=green&gt;Connection Established&lt;br&gt;&lt;/font&gt;"; } else { echo"Connection not established:&lt;br&gt;"; print_r(sqlsrv_errors()); } //Func calls sqlsrv_query($conn, [$symbol],[$DatabaseName]) $stmt = sqlsrv_query($conn,query(array('sym1','sym2','sym3'), "electronic") ); errorCheck($stmt); printTables("Electronic Commodity Prices", $stmt); $stmt = sqlsrv_query($conn,query(array('sym1','sym2','sym3'), "floor") ); errorCheck($stmt); printTables("Floor Commodity Prices", $stmt); $stmt = sqlsrv_query($conn,query(array('sym1','sym2','sym3',... ,sym19), "natgas") ); errorCheck($stmt); printTables("Natural Gas Commodity Prices", $stmt); sqlsrv_free_stmt($stmt); sqlsrv_close( $conn); //This function prints out the tables function printTables($tableName, $stmt) { echo " $tableName&lt;hr&gt; &lt;table cellspacing ='5' cellpadding = '5'&gt; &lt;tr&gt; &lt;th&gt;Symbol&lt;/th&gt; &lt;th&gt;Product&lt;/th&gt; &lt;th&gt;Last Price&lt;/th&gt; &lt;th&gt;Change&lt;/th&gt; &lt;th&gt;High Price&lt;/th&gt; &lt;th&gt;Low Price&lt;/th&gt; &lt;th&gt;Previous Price&lt;/th&gt; &lt;th&gt;Trade Time&lt;/th&gt; &lt;/tr&gt; "; while($row=sqlsrv_fetch_array($stmt)) { echo " &lt;tr&gt; &lt;td&gt;$row[symbol]&lt;/td&gt; &lt;td&gt;&lt;font size =+3 color = blue&gt;$row[description]&lt;/font&gt;&lt;/td&gt; &lt;td&gt;$row[last]&lt;/td&gt; &lt;td&gt;&lt;font size =+5&gt; $row[change]&lt;/font&gt;&lt;/td&gt; &lt;td&gt;$row[highPrice]&lt;/td&gt; &lt;td&gt;$row[lowPrice]&lt;/td&gt; &lt;td&gt;$row[previousprice]&lt;/td&gt; &lt;td&gt;" . date("j M g:i",strtotime($row['tradetime'])) . "&lt;/td&gt; &lt;/tr&gt; "; } echo"&lt;/table&gt;&lt;hr&gt;"; } function query($symbols, $db) { $count = count($symbols); $stmt = " select distinct id,symbol,description,last,change,highPrice,lowPrice,previousprice,tradetime from $db where "; for($i = 0; $i&lt; $count; $i++) { $stmt .= "id in (select MAX(id)from $db where symbol ='$symbols[$i]') "; if($i != $count-1) { $stmt.= "or "; } } $stmt .= "order by description asc"; // id in (select MAX(id)from $db where symbol ='$symbols[0]') // or id in (select MAX(id)from $db where symbol ='$symbols[1]') // or id in (select MAX(id)from $db where symbol ='$symbols[2]') // order by description asc return $stmt; } function errorCheck($stmt) { if( $stmt=== false ) { echo "Error in statement preparation/execution.\n"; die( print_r( sqlsrv_errors(), true)); } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload