dtxq82489 2019-04-21 05:36
浏览 311

无效的数据源名称 - 使用php,PDO和DSN连接到MySQL

This script

<?php
/* Connect to a MySQL database using driver invocation */
$dsn = 'mysqldg';
$user = 'odbc_dg';
$password = '999999999';

try {
    $dbh = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}

?>

gives the error Connection failed: invalid data source name

I have created an entry in /etc/odbc.ini as follows:

[mysqldg]
Description = DGDB 
Driver = mysql537
Database = dg1
Servername = 99.99.99.99
UID = odbc_dg
PWD = 999999
SSLKeyFile = /etc/mysql/ssl/ck.pem
SSLCertFile = /etc/mysql/ssl/cc.pem
SSLCAFile = /etc/mysql/ssl/c1.pem

/etc/odbcinst.ini has the following entry:

[mysql537]
Description = MySQL driver for Plesk
Driver      = /usr/lib/odbc2/lib/libmyodcb5w.so
Setup       = /usr/lib/odbc2/lib/libmyodbc5w.so

The entry in odbcinst.ini works with a non-DSN connection.

I'm obviously missing something, can anyone help? Thanks.

UPDATED....

I have tried Your Common Sense's code as follows:

<?php

$host = '46.99.199.199';
$db   = 'dg';
$user = 'odbc_dg';
$pass = '999999';
$charset = 'utf8mb4';
$options = array(

    PDO::MYSQL_ATTR_SSL_KEY => '/etc/mysql/ssl/ck.pem',
    PDO::MYSQL_ATTR_SSL_CERT => '/etc/mysql/ssl/cc.pem',
    PDO::MYSQL_ATTR_SSL_CA => '/etc/mysql/ssl/c1.pem'
);


$dsn = "mysql:host=$host;dbname=$db;charset=$charset";

try {
    $pdo = new PDO($dsn, $user, $pass, $options);
} catch (PDOException $e) {
    echo 'Connection failed: ' . $e->getMessage();
}


?>

... but I get a connection fail message - Connection failed: SQLSTATE[HY000] [2002]

I think the problem is that the keys I have supplied only work with ODBC

For example this code, which uses odbc_connect works....

<?php

ini_set ('error_reporting', E_ALL);
ini_set ('display_errors', '1');
error_reporting (E_ALL|E_STRICT);

$user = "odbc_dg";
$pass = "99999";

$connection = "Driver=  {mysql537};Server=46.99.199.199;Database=dgdb;UID=dgdb;PWD=999999;sslca=/etc/mysql/ssl/c1.pem;sslkey=/etc/mysql/ssl/ck.pem;sslcert=/etc/mysql/ssl/cc.pem";

$con = odbc_connect($connection, $user, $pass);

$sql="SELECT Id from stk_item"; 
$rs=odbc_exec($con,$sql);

if (!$rs)   {
    exit("Error in SQL");
}

echo "<table><tr>";
echo "<th>Companyname</th>";
echo "<th>Contactname</th></tr>";

while (odbc_fetch_row($rs)) {
   echo odbc_result($rs, "Id"), "
";
}


odbc_close($con);
echo "</table>";

?>

My problem is that I want to connect to the remote database via pdo because that's the only type of connection allowed in the Drupal synchronising code.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?