dongyun3335 2012-04-26 18:54
浏览 70
已采纳

PDO在PHP中执行

Iam having some trouble with a PDO execute statement. My code looks like this:

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");
}

It dosen't print "fail" if i make a print before the $stmt->execute(); it prints the test, but if i make a print just after the execute line, it dosen't print the text.

Any ideas?

UPDATED:

I tried to throw the more generic Exception, without any luck, i implemented PDO::PARAM_INT where i am using integers. And also added the line:

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

Now my code looks like this:

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");
}

I quess it would help to see my db table design?

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 ;

Now i am getting this error:

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条回答 默认 最新

  • duansha7025 2012-04-26 23:04
    关注

    With addition to andrewsi's comment about "MySQL reserved words"... [ssl is indeed a MySQL reserved word!]

    As it stands your create table statement also fails as is, but formatted like so; does not

    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 ;
    

    Although, as I've been writing this I notice that StackOverflow uses the spacing grave for formatting! ( ` )(&#096)

    So, in conclusion change your prepare statement line to the following:

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

    Note : only formatted this way for easier reading :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度