You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CentOS7.9下PHP7.4.5连接SQL Anywhere17报错求助

CentOS 7.9下PHP 7.4.5连接SQL Anywhere 17的连接字符串错误排查

环境与问题概述

CentOS 7.9系统,PHP 7.4.5命令行模式,已安装SQL Anywhere 17客户端并加载64位sqlanywhere扩展及libdbcapi.so。通过其他工具可正常连通目标服务器端口,排除网络问题,但新旧服务器均出现连接报错。

测试代码

<?php

$server = "192.168.56.105";
$port = "2638";
$username = "username";
$password = "********";

if( ! extension_loaded('sqlanywhere') ){
    print("SQL ANYWHRE not available.\n");
} else {
    print("SQL ANYWHERE extension loaded.\n");
}

$connString = "Uid=".$username.";Pwd=".$password.";CommLinks=tcpip(host‌​=".$server.";Port=".$port.")";

echo "Connecting to SQL Anywhere with: '$connString'\n";

$conn = sasql_connect($connString);

if(!$conn){
    echo "Error connecting.";
} else {
    echo "Connected.";
    sasql_disconnect($conn);
}

?>

报错信息

执行代码后出现两类错误:

  1. PHP Warning: sasql_connect(): SQLAnywhere: [-832] Connection error: Error in TCPIP port options in /root/scripts/bcliqx/companies_pull.php on line XX
  2. PHP Warning: sasql_connect(): SQLAnywhere: [-95] Parse error: Invalid or missing keyword near 'host‌​' in /root/scripts/bcliqx/companies_pull.php on line XX

已尝试的连接字符串写法

$connString = "Uid=".$username.";Pwd=".$password.";Server=".$serverName.";host‌​=".$server.":".$port.")";
$connString = "Uid=".$username.";Pwd=".$password.";host‌​=".$server.":".$port.")";
$connString = "Uid=".$username.";Pwd=".$password.";host‌​=".$server.";port=".$port;
$connString = "Uid=".$username.";Pwd=".$password.";CommLinks=tcpip(host‌​=".$server.":".$port.")";
$connString = "Uid=".$username.";Pwd=".$password.";CommLinks=tcpip(host‌​=".$server.";Port=".$port.")";

排查与解决步骤

1. 清理连接字符串中的隐藏字符

从报错信息和尝试的代码来看,host‌关键字后存在不可见的零宽空格字符,这会导致SQL Anywhere解析器无法识别关键字。需要手动删除host后面的隐藏字符,重新输入纯文本的host。

2. 使用标准连接字符串格式

SQL Anywhere TCPIP连接的标准格式为:

$connString = "Uid=$username;Pwd=$password;CommLinks=tcpip(host=$server;port=$port)";

注意:

  • 关键字port大小写不敏感,但建议统一小写
  • 不要在host和port之间混用冒号和分号,TCPIP参数内部用分号分隔
  • 避免多余的括号或未闭合的语法

3. 验证环境变量配置

确保SQL Anywhere的库路径被正确加载,可在PHP脚本开头添加环境变量设置:

putenv("LD_LIBRARY_PATH=/opt/sqlanywhere17/lib64");

或在命令行执行以下命令后再运行脚本:

export LD_LIBRARY_PATH=$SQLANY17/lib64:$LD_LIBRARY_PATH

4. 确认扩展加载正确性

在命令行执行以下命令,验证sqlanywhere扩展是否正确加载:

php -m | grep sqlanywhere

同时确保扩展版本为64位,与PHP和SQL Anywhere客户端架构匹配。

5. 简化连接测试

先使用最简化的连接字符串测试,排除其他参数干扰:

$connString = "Uid=username;Pwd=yourpassword;CommLinks=tcpip(host=192.168.56.105;port=2638)";

如果连接成功,再逐步添加其他参数。

内容的提问来源于stack exchange,提问作者Steffen Scheibler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 15:40:41