gpt4 book ai didi

php - MYSQL 到 sqlsrv

转载 作者:行者123 更新时间:2023-11-29 13:01:58 26 4
gpt4 key购买 nike

正在将我的 mysql php 代码转换为 sqlsrv,但在查找相同函数时遇到问题。

这是代码:

   $result=sqlsrv_query($conn,$sql) or die("Couldn't execute query:<br>" . sqlsrv_error(). "<br>" . sqlsrv_errno()); 


$file_ending = "xls";
$reals=array();
//header info for browser
header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=$filename.xls");
header("Pragma: no-cache");
header("Expires: 0");
/*******Start of Formatting for Excel*******/
//define separator (defines columns in excel & tabs in word)
$sep = "\t"; //tabbed character

//start of printing column names as names of MySQL fields

/*for ($i = 0; $i < sqlsrv_num_fields($result); $i++) {
$type = sqlsrv_field_metadata($result,$i);
echo sqlsrv_field_metadata($result,$i) . "\t";
if ($type == "real")
{
$reals[] = $i;
}
}
*/
$i=0;
foreach( sqlsrv_field_metadata( $result ) as $fieldMetadata ) {
echo $fieldMetadata["Name"]+"\t";
if($fieldMetadata["Type"]=="real")//$fieldMetadata["Type"]=== SQL_REAL
{
$reals[] = $i;
}
$i++;
}


print("\n");
//end of printing column names
//start while loop to get data
while($row = sqlsrv_num_rows($result))
{
$schema_insert = "";
for($j=0; $j<sqlsrv_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != ""){
if (in_array($j, $reals)){
$schema_insert .= str_replace(".",",","$row[$j]").$sep;
} else {
$schema_insert .= "$row[$j]".$sep;
}
}
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert = preg_replace("/\r\n|\n\r|\n|\r/", " ", $schema_insert);
$schema_insert .= "\t";
print(trim($schema_insert));
print "\n";

}

我不确定 sqlsrv 中的 field_type 和 field_name 是否相等,但这不起作用:

for ($i = 0; $i < sqlsrv_num_fields($result); $i++) {
$type = sqlsrv_get_field($result,$i);
echo sqlsrv_get_field($result,$i) . "\t";
if ($type == "real")
{
$reals[] = $i;
}
}

最佳答案

我是一名 ASP.Net 开发人员,对 PHP 知之甚少。也许像下面这样的东西对你有用。只需检查一次。

$stmt = sqlsrv_prepare( $conn, $sql );
$result=sqlsrv_query($conn, $sql);
$i=0;

foreach( sqlsrv_field_metadata( $stmt ) as $fieldMetadata ) {
echo $fieldMetadata["Name"]+"\t";
if($fieldMetadata["Type"]=="real")//$fieldMetadata["Type"]=== SQL_REAL
{
$reals[] = $i
}
$i++;
}

在此处发布之前我还没有运行过它,如果它不起作用,请告诉我。

关于php - MYSQL 到 sqlsrv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23214652/

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