php 未找到数据源名称,且未指定默认驱动程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26290532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 18:15:46  来源:igfitidea点击:

Data source name not found, and no default driver specified

phpsqlsql-serverodbc

提问by Kinfe

I need help fixing an error: SQL state IM014 in SQLConnectand SQL state IM002 in SQLConnect.

我需要帮助修复错误:SQL state IM014 in SQLConnectSQL state IM002 in SQLConnect

I run the same script, one on webserver/remote/and the other one from the local Machine trying to access the same database but i get different error message.

我运行相同的脚本,一个在webserver/remote/本地机器上,另一个从本地机器尝试访问同一个数据库,但我收到不同的错误消息。

When i run it from web server i get

当我从网络服务器运行它时,我得到

SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQL

SQL 错误:[unixODBC][驱动程序管理器]未找到数据源名称,且未指定默认驱动程序,SQL 中的 SQL 状态为 IM002

where as when i run it on local machine i get

当我在本地机器上运行它时,我得到

[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

[Microsoft][ODBC 驱动程序管理器] 指定的 DSN 包含驱动程序和应用程序之间的体系结构不匹配

I used the following code in php script to connect to the Local database

我在php脚本中使用以下代码连接到本地数据库

$odbc['dsn'] = "SageLine50v19";
$odbc['user'] = "Peac";
$odbc['pass'] = "XXXX";
$mysql['host'] = "localhost";
$mysql['user'] = "root";
$mysql['pass'] = "";
$mysql['dbname'] = "sagetest";
$mysql['idfield'] = "id";


// Step 1: Connect to the source ODBC database
if ($debug) echo "Connect to " . $odbc['dsn'] . ' as ' . $odbc['user'] . "\n";
$conn = odbc_connect($odbc['dsn'], $odbc['user'], $odbc['pass']);
if (!$conn) {
die("Error connecting to the ODBC database: " . odbc_errormsg());
}

// loop through each table 
$allTables = odbc_tables($conn);
$tablesArray = array();
while (odbc_fetch_row($allTables)) {
 if (odbc_result($allTables, "TABLE_TYPE") == "TABLE") {
    $tablesArray[] = odbc_result($allTables, "TABLE_NAME");
 }
}
 //print_r($tablesArray);      // to list all tables

My ODBC.inilooks like below

我的ODBC.ini如下所示

[ODBC 32 bit Data Sources]
manager=Sage Line 50 v16 (32 bit)
t=SQL Server Native Client 10.0 (32 bit)
s1=Pervasive ODBC Client Interface (32 bit)
SageLine50v19=Pervasive ODBC Client Interface (32 bit)
[manager]
Driver32=C:\Windows\SysWOW64\S16DBC32.dll
[t]
Driver32=C:\Windows\system32\sqlncli10.dll
[s1]
Driver32=C:\Program Files (x86)\Pervasive Software\PSQL\bin\w3odbcci.dll
[SageLine50v19]
Driver32=C:\Program Files (x86)\Pervasive Software\PSQL\bin\w3odbcci.dll

回答by Malbordio

Very simple!

很简单!

On server:

在服务器上:

SQL error: [unixODBC][Driver Manager]Data source name not found, and no default driver specified, SQL state IM002 in SQL

SQL 错误:[unixODBC][驱动程序管理器]未找到数据源名称,且未指定默认驱动程序,SQL 中的 SQL 状态为 IM002

Delete the USER data source name and define it as a SYSTEM data source name. Go to -> Start -> Settings -> Control Panel -> Administrative Tools -> Data Sources (ODBC): - User DSN : delete the specified User DSN - System DSN : create a new System DSN

删除 USER 数据源名称并将其定义为 SYSTEM 数据源名称。转到 -> 开始 -> 设置 -> 控制面板 -> 管理工具 -> 数据源 (ODBC): - 用户 DSN:删除指定的用户 DSN - 系统 DSN:创建新的系统 DSN

Else try to install same 32bits or 64bits version as locally installed.

否则尝试安装与本地安装相同的 32 位或 64 位版本。

Locally:

本地:

[Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

[Microsoft][ODBC 驱动程序管理器] 指定的 DSN 包含驱动程序和应用程序之间的体系结构不匹配

32bit/64 bit conflict - either you install a 32bit driver version or you use/configure the 32bit under C:\Windows\SysWoW64\

32 位/64 位冲突 - 要么安装 32 位驱动程序版本,要么在 C:\Windows\SysWoW64\ 下使用/配置 32 位

Try to install same 32bits or 64bits version as locally installed.

尝试安装与本地安装相同的 32 位或 64 位版本。

回答by Yash

First open the administrative tools option. Check your operating system. If you are using 64-bit then select driver of 64-bit. Or it is 32-bit then select 32-bit.

首先打开管理工具选项。检查您的操作系统。如果您使用的是 64 位,则选择 64 位驱动程序。或者它是 32 位然后选择 32 位。

Please do not make any user DSN. Select system DSN.

请不要制作任何用户DSN。选择系统 DSN。

e.g. I made System DSN which name is "empcon1" then in PHP code you should write like this:

例如,我创建了名称为“empcon1”的 System DSN,然后在 PHP 代码中你应该这样写:

$dbNewName = "empcon1"; <br/>
$dbUserName = "yash"; <br/>
$dbPassword = "yash";<br/>
$conn=odbc_connect($dbNewName,$dbUserName,$dbPassword);

thats all.. You will get connected.

仅此而已.. 您将获得连接。

回答by Rotti

There are a few things that can cause this. First, the DSN needs to be declared on both machines, on the remote machine it needs to be a WAN or LAN address depending on where it lives in the network. Second you need to make sure you have the right ODBC driver, there are 32 bit drivers and 64 bit drivers. MySQL connector ships with both.

有一些事情可能会导致这种情况。首先,需要在两台机器上声明 DSN,在远程机器上,它需要是 WAN 或 LAN 地址,具体取决于它在网络中的位置。其次,您需要确保拥有正确的 ODBC 驱动程序,有 32 位驱动程序和 64 位驱动程序。MySQL 连接器与两者一起提供。

32 bit ODBC: %systemdrive%\Windows\SysWoW64\odbcad32.exe
64 bit ODBC: %systemdrive%\Windows\system32\odbcad32.exe

I would try removing the 64 bit driver, adding the 32 bit driver and see how that goes. Also, make sure you test your ODBC to make sure you have connection. If you after than then time to check the coding.

我会尝试删除 64 位驱动程序,添加 32 位驱动程序,看看效果如何。此外,请确保您测试您的 ODBC 以确保您有连接。如果您之后再检查编码。

回答by jgarcias

In my case, I was trying to connect a PHP script to a MS Access 2003 database (.mdb file) using the code I found somewhere else:

就我而言,我试图使用我在其他地方找到的代码将 PHP 脚本连接到 MS Access 2003 数据库(.mdb 文件):

$dbName = "C:\DB\myAccessFile.mdb";
if (!file_exists($dbName)) {
    die("Could not find database file.");
}
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb, *.accdb)}; charset=UTF-8; DBQ=$dbName; Uid=myUserName; Pwd=secret;");

// use the connection here
$result = $db->query('SELECT * FROM tableName');
$counter = 1;
while ($row = $result->fetch()) {
    echo $row["firstName"] . " " . $row['lastName'];
    echo '<br/>';
    $counter = $counter + 1;
    if ($counter == 50) {
       break;
    }
}

// and now we're done; close it
$sth = null;
$dbh = null;

Operating system: Windows 10 Pro.

操作系统:Windows 10 专业版。

WAMP server version: 3.1.9

WAMP 服务器版本:3.1.9

Just make sure the driver specified in the code matches exactly the driver name in the ODBC dialog box, as shown in this image:

只需确保代码中指定的驱动程序与 ODBC 对话框中的驱动程序名称完全匹配,如下图所示:

enter image description here

在此处输入图片说明