I have lately began exploring using AJAX for any small project, and I have had reasonable success though it is not as smooth as I'd like.

The Fundamental setup is the fact that an programs named ProphetX which connects with Stand out to exhibit stock exchange prices. Costs are up-to-date because they alternation in Stand out. Using VBA I save the information in the spreadsheet into an SQL08 DB whenever a cost updates. This might be a couple of occasions per second.

Using PHP with an Apache server I connect with the SQL DB and load the information into tables, along with a javascript function to help keep upgrading the data once every second. I have observed however that sometimes the page only will hang if you have up, or load an empty screen should you pull up, specially when information is being up-to-date quickly. To the meat of my question: Can there be something during my code that could be leading to this hiccup? I doubt it's congesting the network or server assets as I have been monitoring them plus they appear low.

Also I made use of WireShark to watch network traffic, so when I stock up the page also it shows blank within the browser, I can tell the HTML being sent in the server to my machine.

Any help / coding style critique is a lot appreciated, source code below.

Index.php:

<html>
<head>
<script type="text/javascript" src="update.js"></script>
</head>
<body onLoad = update()>
<font size = +2>
<?php
echo "
<div id = 'marketData'></div>
";
?>
</font>
</body></html>

Update.js:

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 && 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);
}

Update.php:

<?php
//connect to database
error_reporting(0);
    sqlsrv_configure("WarningsReturnAsErrors", 1);
    $server = "myServer";
    $db = "myDB";
    $connectionInfo = array("Database"=>"$db, "UID"=>"user", "PWD"=>"pass");
    $conn = sqlsrv_connect($server, $connectionInfo);

    if($conn)
    {
        echo "<font size =-1 color=green>Connection Established<br></font>";
    }
    else
    {
        echo"Connection not established:<br>";
        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<hr>
        <table cellspacing ='5' cellpadding = '5'>
        <tr>
            <th>Symbol</th>
            <th>Product</th>
            <th>Last Price</th>
            <th>Change</th>
            <th>High Price</th>
            <th>Low Price</th>
            <th>Previous Price</th>
            <th>Trade Time</th>
        </tr>
        ";
        while($row=sqlsrv_fetch_array($stmt))
        {
        echo 
        "
        <tr>
            <td>$row[symbol]</td>
            <td><font size =+3 color = blue>$row[description]</font></td>
            <td>$row[last]</td>
            <td><font size =+5> $row[change]</font></td>
            <td>$row[highPrice]</td>
            <td>$row[lowPrice]</td>
            <td>$row[previousprice]</td>
            <td>" .  date("j M g:i",strtotime($row['tradetime']))  . "</td>
        </tr>
        ";
        }
        echo"</table><hr>";
    }
    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< $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));
        }
    }
?>