无效数据源 PHP PDO Mysql

问题描述 投票:0回答:1
<?php
#require_once('inc/dbc1.php');
$dsn = 'mysql:dbname=dbname;host=somehost;
$user = 'someuser';
$password = 'SomePass';

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

$pdo1 = new PDO($dsn, $user, $password);
$pdo1->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$sth1 = $pdo1->prepare('SELECT pID, lname, fname FROM Professor ORDER BY pID DESC LIMIT 5;');
$sth1->execute(array());
?>

抛出错误:

Uncaught exception 'PDOException' with message 'invalid data source name' in  PDO->__construct('', NULL, NULL) on line 1

有人发现这有什么问题吗?

php mysql pdo
1个回答
9
投票

社区警告:此答案与问题中提供的错误消息无关,因此对此类错误没有帮助。

你有

$dsn = 'mysql:dbname=dbname;host=somehost;

也许只是也许...

$dsn = 'mysql:dbname=dbname;host=somehost';

除非这是剪切和粘贴问题时的错误。

© www.soinside.com 2019 - 2024. All rights reserved.