PHP& SQL Server-字段名称被截断 [英] PHP & SQL Server - field names truncated

查看:112
本文介绍了PHP& SQL Server-字段名称被截断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是相关代码:

function connect(){
    // DB credentials and info defined here....
    $connection = odbc_connect("DRIVER={SQL Server Native Client 11.0}; Server=$server; Database=$db;", $loginname, $loginpass);
    return $connection;
}

function odbc_fetch_results($stmt, &$results) {
    $numrows = odbc_num_rows($stmt);
    $row = odbc_fetch_array($stmt);
    print_r($row); // Prints: Array ( [MEASUREMENT_UNI] => kg)
    if($row){
         $results = array ($row);
         while( $row = odbc_fetch_array($stmt)){
            array_push($results, $row);
         }
    }
    return $numrows;
}

$sql = "select * from measurements where ID=$id";
$stmt = executeSQL($conn,$sql);
$nrows = odbc_fetch_results($stmt, $results);
odbc_free_result($stmt);
print_r($result[0]); // Prints: Array ( [0] => Array ( [MEASUREMENT_UNI] => kg) ) 

结果应包含一个名为MEASUREMENT_UNIT的列,该列(当我执行我可以验证的print_r时)将被截断为仅15个字符的MEASUREMENT_UNI.最后一个字母T被剪掉.

The result should contain a columnn called MEASUREMENT_UNIT which (when I do a print_r I can verify) is truncated to MEASUREMENT_UNI which is only 15 characters. The last letter T is cut off.

我还尝试了在SQL Server数据库上使用 different 表和 different 列进行查询的测试,以确保使用我正在使用的特定表或列.我验证了在不同的表/列中也发生了同样的事情:如上所述,当我运行选择查询时,列名被截断为最多15个字符.

I also tried a query with a different table and a different column on the SQL Server database as a test to make sure it wasn't any strange setup with the particular table or column that I'm working with. I verified the same thing occurs with a different table/column: column names are truncated to 15 characters max when I run a select query as above.

我还尝试了一个select方法,该方法指定了像select MEASUREMENT_UNIT from from measurements where ID=$id这样的字段名称,而不是select *,但这也不能解决问题.

I have also tried a select which specifies the field name like select MEASUREMENT_UNIT from from measurements where ID=$id instead of select * but that doesn't solve the problem either.

我在这里也看到过其他类似的帖子,但它们似乎都表明我应该能够至少获得30个字符,而不是我看到的15个字符的限制.

I've seen other similar posts here about this but they all seem to indicate that I should be able to get at least 30 characters, not the 15 character limit that I'm seeing.

为什么列名被截断为15个字符?

Why is the column name being truncated to 15 characters?

编辑:连接到MySQL服务器数据库似乎没有导致相同的问题. MySQL表中的数据库列名称没有被截断,这使我相信ODBC插件不是问题.

Edit: Connecting to a MySQL server database did not seem to result in the same problem. DB column names from the MySQL tables were NOT truncated which leads me to believe that this is not a problem with the ODBC plugin.

$connection = odbc_connect("DRIVER={MySQL};Server=$server; Database=$db;", $loginname, $loginpass);
$sql = "select * from measurements where ID=$id";
$stmt = executeSQL($conn,$sql);
$nrows = odbc_fetch_results($stmt, $results);
odbc_free_result($stmt);
print_r($result[0]); // Prints CORRECTLY: Array ( [0] => Array ( [MEASUREMENT_UNIT] => kg) )

请注意,以上两个代码节均在具有相同PHP + ODBC安装的同一服务器上的同一文件中进行了测试.

Note that both of the above code sections were tested in the same file on the same server with the same PHP + ODBC installation.

推荐答案

该问题肯定是Microsoft ODBC驱动程序版本11并已在

The problem is definitely with the Microsoft ODBC drivers version 11, and are fixed in ODBC Driver 13 Preview for SQL Server.

我发现这是因为运行安装了Driver 13 Preview的运行ubuntu 14.04的开发机器可以正常工作,但是随后当我将其部署到运行带有Driver 11的RHEL 7的生产服务器时,由于列名被截断,因此造成了各种各样的破坏15个字符.

I discovered this as my development machine running ubuntu 14.04 with the Driver 13 Preview installed works fine, but then when I deployed to our production server running RHEL 7 with the Driver 11, all kinds of havoc ensued as column names were truncated at 15 chars.

Microsoft的文档对Linux的支持不佳,因此,如果您运行的是ubuntu,则这里的要点安装它.

Microsoft's documentation is lackluster for Linux support, so if you're running ubuntu, then here's the gist of getting it installed.

这篇关于PHP& SQL Server-字段名称被截断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆