mysqli到postgres数据库

时间:2015-10-30 09:45:58

标签: php database postgresql mysqli

我创建了一个脚本,用于将数据从mysqli数据库插入到postgres数据库

<?php
      $conn = new mysqli(...........);
      if ($conn->connect_error) {
      die("Connection fialed: " . $conn->connect_error);
      }
      else
      {
      echo "MySQL connection successfull\n";
      }
      $db = pg_connect(.................);
      if(!$db){
      echo "Error : Unable to open database\n";
      } else {
      echo "PostgreSQL connection successfull\n";
       }
      $sql = "SELECT * FROM crm_subscriptions";
      echo $sql . "\n";
      $result = $conn->query($sql);
      echo $conn->error . "\n";
      if ($result->num_rows > 0) {
       while($row = $result->fetch_assoc()) {
        $psql = "
        INSERT INTO subscriptions_sub (id,cust_id,tarrif_id,des,datetime_created,datetime_modified,credit,credit_limit,status,date_start,date_end,backoffice_userref,backoffice_deleted,backoffice_createdon,backoffice_createdby, backoffice_updatedon,backoffice_updatedby,backoffice_administration,backoffice_readonly,backoffice_order,backoffice_dummy)
        VALUES('" .(int) $row["subscription_id"] . "','" . (int) $row["customer_id"] . "','" . (int) $row["tariffplan_id"] . "','" . $row["description"] . "',NULLIF('" . $row["datetime_created"] . "','0000-00-00 00:00:00')::timestamp,NULLIF('" . $row["datetime_modified"] . "','0000-00-00 00:00:00')::timestamp,'" . (int) $row["credit"] . "','" . (int) $row["credit_limit"] . "','" . $row["status"] . "',NULLIF('" . $row["date_start"] . "','')::timestamp,NULLIF('" . $row["date_end"] . "','')::timestamp,'" . (int) $row["backoffice_userref"] . "','" . (int) $row["backoffice_deleted"] . "',NULLIF('" . $row["backoffice_createdon"] . "','0000-00-00 00:00:00')::timestamp,'" . (int) $row["backoffice_createdby"] . "',NULLIF('" . $row["backoffice_updatedon"] . "','0000-00-00 00:00:00')::timestamp,'" . (int) $row["backoffice_updatedby"] . "','" . (int) $row["backoffice_administration"] . "','" . (int) $row["backoffice_readonly"] . "','" . (int) $row["backoffice_order"] . "','" . (int) $row["backoffice_dummy"] . "');
        ";
        print_r($row);      
        $ret = pg_query($db, $psql);
        if(!$ret){
            echo pg_last_error($db);
        } else {
            echo "Records created succesfully\n";
        }       
        }
        } else {
    echo "0 results \n";
      }
       ?>

当我运行它时,我收到此错误:

  

类型为timestamp的输入语法无效:“”in   第38行/home/ruud/Database2.php
错误:输入语法无效   对于类型时间戳:“”root @ 149-210-204-94:/ home / ruud#
错误:   类型为timestamp的输入语法无效:“”Array

datetime_created和datetime_modified来自TIMESTAMP类型date_start,而date_end来自DATE类型backoffice_createon和backoffice_update ,来自DATETIME类型

1 个答案:

答案 0 :(得分:0)

你需要安装一个postgres驱动程序/模块,因为mysqli是mysql的驱动程序。

sudo apt-get install php5-pgsql

或者如果安装了软件包,则需要在php.ini中启用de module

extension=php_pgsql.dll (windows)
extension=php_pgsql.so (linux)

您可能已经安装了它。检查您的phpinfo();