将SQL结果输出到表

时间:2012-11-15 20:47:03

标签: javascript html sql html5 adodb

如何在下面修改代码,以便将SQL查询的结果动态生成到想要下面的示例表的表中? (每桌子2件)

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
    function test() {
        try {
            alert("running function test")
        var cn      = new ActiveXObject("ADODB.Connection")
        var rs      = new ActiveXObject("ADODB.Recordset")
        var sql     = "SELECT * FROM tbl_rssims"
        var db      = "G:\\AS\\Asf\\ASF\\RSSIMS\\db\\rssims.mdb"

        cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source = " + db + "")
        rs.Open(sql, cn, 1, 3)

        var html    =  '<!DOCTYPE html>\n'
            html    +=  '<html>\n'
            html    +=  '<head>\n'
            html    +=  '<table style="border: none; table-layout: fixed; width: 100%; text-align: left;" cellpadding="0" cellspacing="0">\n'

            //<!-- WRITE FIELD VALUES -->
            while (!rs.eof) {
                html += '<tr>\n';
                for (var c = 0; c < rs.fields.count; ++c) {
                    html += '<td>' + rs.fields(c).value + '</td>\n'
                }//end of for
                html += '</tr>\n'
                rs.movenext
            }//end of while
            html += '</table>'
            window.open('','').document.write(html)

        rs.close
        cn.close
    }//end of try
    catch(e) {
        alert(e.description)
    }
}//end of function

</script>
</head>
<body>
    <b>Example:</b>
    <table style="border: none; table-layout: fixed; width: 100%; text-align: left;" cellpadding="0" cellspacing="0">
        <tr>
            <td>Mr. Ronald McDonald<br>Chief Executive Officer<br>The Hudson Bay Corporation<br>123 Yahoo Street<br>Toronto, Ontario<br>Canada</td>
            <td>Mr. Steve Marin<br>Chief Executive Officer<br>General Motors<br>456 Don Mills Street<br>Toronto, Ontario<br>Canada</td>
        </tr>
    </table>
    <input onclick="test()" type="button" value="button" id="button">
</body>
</html>

2 个答案:

答案 0 :(得分:0)

这个怎么样:

html += '<tr><td>' + rs.GetString(2, -1, '<br>', '</td><td>', '') + '</td></tr>';

您希望每列之间<br>,每行之间</td><td>

答案 1 :(得分:0)

尝试这样的事情..

<php?
$query='select * from table_name';
$result=mysql_query($query);
?>
<table>
<tr>
<th>ID </th>
<th>Name</th>
</tr>
<?php
while($row=mysql_fetch_assoc($result)){
    echo '<tr>
    <td align="center">'.$row['id'].'-'.$row['sID'].'</td>
    <td align="center">'.$row['name'].'</td>
    </tr>';
}
?>
</table>