【问题标题】:Drupal 7 PDOExceptionDrupal 7 PDO异常
【发布时间】:2011-08-10 20:27:00
【问题描述】:

我制作了一个脚本,可以从一个数据库中导入内容并将其放入另一个数据库;问题是它与消息中断:

PDOException:在 hcap_import_do() 中(/mnt/hgfs/docroot/hcapdrupal/site_drupal/public_html/sites/all/modules/custom/hcap/hcap.module 的第 215 行)。

代码如下:

db_insert('hcap_candidates')->fields(array(
  'id' => $e['old_id'],
  'pic' => $e['logo'],
  'placed' => $e['placed'],
  'title' => $e['title'],
  'name' => $e['name'],
  'interim' => $e['interim'],
  'permanent' => $e['permanent'],
  'birth' => $e['birth'],
  'marital_status' => $e['marital'],
  'adress' => $e['address'],
  'city' => $e['city'],
  'postal' => $e['postal'],
  'phone_home' => $e['phone_home'],
  'phone_work' => $e['phone_work'],
  'phone_mobile' => $e['phone_mobile'],
  'email1' => $e['email1'],
  'email2' => $e['email2'],
  'reg_by' => $e['reg_by'],
  'cand_head' => $e['cand_head'],
  'cand_body' => $e['cand_body'],
  'prominent' => $e['prominent'],
  'broadcast' => $e['broadcast'],
  'cv' => $e['cv'],
  'old_id' => $e['old_id']
))->execute();  // This is line 215.

有没有人知道什么是错的?

【问题讨论】:

    标签: database drupal drupal-7


    【解决方案1】:

    您可以使用“try ... catch”块来了解有关错误的更多信息。我需要更多周边代码和表结构来理解查询失败的原因,但语法似乎是正确的。

    如果您不确定,下面是 PHP 中“try ... catch”块的样子:

    <?php
    try {
    db_insert('hcap_candidates')
      ->fields(
        array(
          'id' => $e['old_id'],
          'pic' => $e['logo'],
          'placed' => $e['placed'],
          'title' => $e['title'],
          'name' => $e['name'],
          'interim' => $e['interim'],
          'permanent' => $e['permanent'],
          'birth' => $e['birth'],
          'marital_status' => $e['marital'],
          'adress' => $e['address'],
          'city' => $e['city'],
          'postal' => $e['postal'],
          'phone_home' => $e['phone_home'],
          'phone_work' => $e['phone_work'],
          'phone_mobile' => $e['phone_mobile'],
          'email1' => $e['email1'],
          'email2' => $e['email2'],
          'reg_by' => $e['reg_by'],
          'cand_head' => $e['cand_head'],
          'cand_body' => $e['cand_body'],
          'prominent' => $e['prominent'],
          'broadcast' => $e['broadcast'],
          'cv' => $e['cv'],
          'old_id' => $e['old_id'],
        )
      )
      ->execute();
    }
    catch (PDOException $e) {
      print_r($e->getMessage());
    }
    ?>
    

    您的查询可能存在一个简单问题(没有名为hcap_candidates 的表,该表中没有名为marital_status 的字段等)。异常消息应该告诉你所有你需要知道的。

    顺便说一句,print_r 只是在浏览器中查看输出的一种快捷方式。也可以使用watchdogdrupal_set_message...

    【讨论】:

    • try 方法实际上产生了一个 SQLSTATE 错误,这正是我想要的。非常感谢!
    • try catch 是解决 drupal 错误的好主意...谢谢!
    【解决方案2】:

    我不知道确切的问题,但请确保您实施了以下步骤

    1- 您在 settings.php 文件中定义了 2 个数据库

    2- 你使用 db_set_active("database2name");在将数据插入 {database2name}

    之前

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-29
      • 2017-06-03
      • 1970-01-01
      • 2012-09-19
      • 2014-06-19
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多