您好,欢迎光临! 推荐您使用Chrome浏览器访问本站。

php利用ODBC连接sql2008数据库并通过ajax实时更新

服务端代码: 

<?php 
 
$connection = odbc_connect("Driver={SQL Server Native Client 10.0};Server=WINDOWS-01\SQLEXPRESS;Database=backups;""php""123456"); 
if (!$connection){ 
    exit("Connection Failed: " . $connection); 
} 
 
$sql="SELECT pro_name,ask_price,bid_price FROM t_item"$rs=odbc_exec($connection,$sql); 
if (!$rs){ 
    exit("Error in SQL"); 
} 
 
echo "<h3>商品报价数据实时更新</h3><table><tr>"echo "<th>类别</th>"echo "<th>卖价</th><th>买价</th></tr>"; 
 
while (odbc_fetch_row($rs)){ 
    $pro_name=odbc_result($rs,"pro_name"); 
    $ask_price=odbc_result($rs,"ask_price"); 
    $bid_price=odbc_result($rs,"bid_price"); 
    echo "<tr><td>$pro_name</td><td>$ask_price</td>"; 
    echo "<td>$bid_price</td></tr>"; 
} 
echo "</table>"; 
odbc_close($connection); 
 
?> 

前端代码(index.html): 

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Ajax Web</title> 
    <script src="jquery/jquery-1.8.0.js" type="text/javascript" ></script> 
    <script> 
        function getD(){ 
        $.post("http://127.0.0.1/getdata.php", 
                { 
                }, 
                function(data){ 
                    //返回数据处理 
                    $("#loading").html(data); 
                }); 
        setTimeout('getD()',3000); 
        } 
        $(function(){ 
            getD(); 
        }); 
     
    </script> 
</head> 
<body> 
    <div id="wrapper"> 
        <p id="fxdata"><span id="loading">加载中...</span></p> 
    </div> 
</body> 
<html> 

您可能也喜欢