2

我在使用 PDO 执行语句时遇到了一些问题。我的代码如下所示:

try {

    $stmt = $this->dbh->prepare("INSERT INTO smtp_servers (host, port, ssl, auth, username, password) VALUES(:host, :port, :ssl, :auth, :username, :password)");
    $stmt->bindParam(':host', $serverOptions[0]);
    $stmt->bindParam(':port', $serverOptions[1]);
    $stmt->bindParam(':ssl', $serverOptions[2]);
    $stmt->bindParam(':auth', $serverOptions[3]);
    $stmt->bindParam(':username', $serverOptions[4]);
    $stmt->bindParam(':password', $serverOptions[5]);
    $stmt->execute();

} catch (PDOException $e) {
    print("fail");
}

如果我在它打印测试之前进行打印,它不会打印“失败” $stmt->execute();,但如果我在执行行之后进行打印,它不会打印文本。

有任何想法吗?

更新:

我试图抛出更通用的异常,但没有任何运气,我PDO::PARAM_INT在使用整数的地方实现了。并且还添加了一行:

$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

现在我的代码如下所示:

try {

    $stmt = $this->dbh->prepare("INSERT INTO smtp_servers (host, port, ssl, auth, username, password) VALUES(:host, :port, :ssl, :auth, :username, :password)");
    $stmt->bindParam(':host', $serverOptions[0]);
    $stmt->bindParam(':port', $serverOptions[1], PDO::PARAM_INT);
    $stmt->bindParam(':ssl', $serverOptions[2], PDO::PARAM_INT);
    $stmt->bindParam(':auth', $serverOptions[3], PDO::PARAM_INT);
    $stmt->bindParam(':username', $serverOptions[4]);
    $stmt->bindParam(':password', $serverOptions[5]);
    $stmt->execute();

} catch (PDOException $e) {
    debug("fail");
}

我想看看我的数据库表设计会有所帮助吗?

CREATE TABLE IF NOT EXISTS `smtp_servers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `host` varchar(50) NOT NULL,
  `port` int(11) DEFAULT NULL,
  `ssl` smallint(11) DEFAULT NULL,
  `auth` smallint(11) DEFAULT NULL,
  `username` varchar(50) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

现在我收到此错误:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ssl, auth, username, password FROM smtp_servers' at line 1' in /var/www/isms/php/communication/Mail.php on line 13 PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ssl, auth, username, password FROM smtp_servers' at line 1 in /var/www/isms/php/communication/Mail.php on line 13 Call Stack: 0.0002 649056 1. {main}() /var/www/isms/index.php:0 0.0023 867336 2. include('/var/www/isms/php/settings/ismsSettings.php') /var/www/isms/index.php:88 0.0065 1091712 3. require_once('/var/www/isms/php/settings/MailSettings.php') /var/www/isms/php/settings/ismsSettings.php:29 0.0147 2288960 4. require_once('/var/www/isms/php/forms/settings/EditSmtpServer.php') /var/www/isms/php/settings/MailSettings.php:4 0.0155 2290456 5. Mail->getServers() /var/www/isms/php/forms/settings/EditSmtpServer.php:41 0.0156 2291888 6. PDOStatement->execute() /var/www/isms/php/communication/Mail.php:13

4

4 回答 4

2

除了andrewsi关于“ MySQL保留字”的评论...... [ssl确实是一个MySQL保留字!]

就目前而言,您的 create table 语句也按原样失败,但格式如下;才不是

CREATE TABLE `smtp_servers` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`host` VARCHAR( 50 ) NOT NULL ,
`port` INT( 11 ) NOT NULL ,
`ssl` SMALLINT( 11 ) NOT NULL ,
`auth` SMALLINT( 11 ) NOT NULL ,
`username` VARCHAR( 50 ) NOT NULL ,
`password` VARCHAR( 50 ) NOT NULL
) ENGINE = MYISAM ;

虽然,当我写这篇文章时,我注意到StackOverflow使用间距坟墓 进行格式化!( ` )(`)

因此,总而言之,将您的准备语句行更改为以下内容:

$stmt = $this->dbh->prepare("INSERT INTO `smtp_servers` 
    (`host`, `port`, `ssl`, `auth`, `username`, `password`) 
    VALUES(:host, :port, :ssl, :auth, :username, :password)");

注意: 仅以这种方式格式化以便于阅读:)

于 2012-04-26T23:04:37.887 回答
1

您是否尝试过打印查询的 SQL,用您尝试插入的实际参数替换命名参数,然后从命令行运行它?

异常的文本暗示 SQL 语句存在问题,而不是 PDO 连接存在问题,从命令行运行它会让您知道 SQL 本身是否有效。

于 2012-04-26T19:51:13.650 回答
1

在您的代码示例中,fail打印应该只在出现问题的情况下发生,即当 try 块内的脚本抛出异常时。

假设一切顺利并且查询成功,那么看不到这条线是一个好兆头。


如果查询没有成功,但您仍然没有看到错误消息,请尝试告诉 PDO 仅在错误时抛出 PDOExceptions:

$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

在实例化 PDO 对象之后。

于 2012-04-26T18:56:50.047 回答
0

这可能是因为您现在处于特定的命名空间中。尝试替换} catch (PDOException $e) {} catch (\PDOException $e) {看看是否有变化。

于 2012-04-26T19:38:26.170 回答