gpt4 book ai didi

php - 在 php 表中显示 MSSQL 选择数据

转载 作者:可可西里 更新时间:2023-10-31 22:14:38 26 4
gpt4 key购买 nike

我确信我遗漏了一些简单的东西。这是我第一次从 MSSQL 服务器中提取数据并使用 php 将其显示在表格中。我过去用 mysql 做过这个,但不能让它与 mssql 一起工作。这是我当前的代码:

<?php
$myServer = "server";
$myUser = "user";
$myPass = "password";
$myDB = "mssqldb";

//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");

//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");

//declare the SQL statement that will query the database
$query = "SELECT col1, col2 ";
$query .= "FROM sqltable ";

//execute the SQL query and return records
$result = mssql_query($query)
or die('A error occured: ' . mysql_error());

//Show results in table

$o = '<table id="myTable">
<thead>
<tr>
<th>Col 1</th>
<th>Col 2</th>
</tr>
</thead><tbody>';

while ( $record = mssql_fetch_array($result) )
{
$o .= '<tr><td>'.$col1.'</td><td>'.$col2.'</td></tr>';
}

$o .= '</tbody></table>';

echo $o;

//Show result from sql table separated by comma (commented out)
/* while ( $record = mssql_fetch_array($result) )
{
echo $record["col1"] . " , " . $record["col2"] . "<br />";
} */

//free result set memory
mssql_free_result($result);

//close the connection
mssql_close($dbhandle);
?>

最佳答案

$col1 和 $col2 变量从未被定义。您应该使用注释部分中的内容,$record["colname"]。

关于php - 在 php 表中显示 MSSQL 选择数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com