简体   繁体   English

如何使用Codeigniter从本地数据库同步到服务器数据库

[英]how to sync from local database to server database using codeigniter

I created PHP app in working condition, clients want this app without internet working in localhost and with internet automatically connect to the live server, I am searching google didn't get any code for sync database from local to the server, below shown that code, for localhost 我创建了处于工作状态的PHP应用程序,客户端希望该应用程序在没有localhost的Internet且没有Internet的情况下自动连接到实时服务器,我正在搜索google,但没有从本地到服务器的任何同步数据库代码,下面显示了该代码,对于localhost

controller:syn.php 控制器:syn.php

<?php class Sync extends CI_Controller {
private $tables;
function __construct() {
    parent::__construct();
    $this->load->database();
    $this->tables = array(
        "test_users" => array("id","name")

    );


    $this->id_store = $this->config->item('id_store');


}

function index() {
    echo '<br>'.date("H:i:s A");
    $newLine = "\r\n";
    $output = $update = "";

    foreach($this->tables as $table_name => $columns ){

        $col_check = "SHOW COLUMNS FROM `{$table_name}` LIKE 'is_sync'";


        $col_exists = $this->db->query($col_check);


        if ( $col_exists->num_rows() > 0 ) { // is_sync column exists for this table 
            $sql = "SELECT * FROM {$table_name} WHERE is_sync = 0 LIMIT 500" ;

            $rows = $this->db->query($sql);


            if ( $rows->num_rows() > 0 ) { // where is_sync = 0
                foreach ( $rows->result_array() as $row ) {

                    $col_val = $update_col_val = $already_exists = array();
                    foreach($row as $name => $val) {
                        if ( is_null( $val ) ) {
                            continue;
                        }
                        $val = $this->db->escape($val);


                        $update_col_val[] = " `{$name}` = '{$val}' ";

                        if ( "is_sync" === $name ) {
                            $val = 1;
                        }

                        if ( "id_store" === $name ) {
                            $val = $this->id_store;
                        }

                        $col_val[] = " `{$name}` = '{$val}'";

                        if ( $name === $columns[0] ){
                            $already_exists[] = " `$columns[0]` = '{$val}'";
                        }

                        if ( $name === $columns[1] ){
                            $already_exists[] = " `$columns[1]` = '{$val}'";
                        }


                    }

                    if ( is_array($col_val) && count($col_val) > 0 ) {

                        //echo $this->table_keys[$table_name];
                        $output .= "SELECT * FROM `{$table_name}` WHERE `id_store` = {$this->id_store} AND " . implode(" AND ", $already_exists) . " ||";
                        $output .= "INSERT INTO `{$table_name}` SET " . implode(",", $col_val) . " ||";
                        $output .= "UPDATE `{$table_name}` SET " . implode(",", $col_val) . " WHERE `id_store` = {$this->id_store} AND " . implode(" AND ", $already_exists);
                        $output .= " ;; ";

                        $update .= "UPDATE {$table_name} SET is_sync = 1 WHERE " . implode(" AND ", $update_col_val) . " ;; ";
                    }
                }
            } // if is_sync = 0

        } // if is_sync column exists 
    }

    if ( !empty($output) && $this->post_data($output) ) {
        $update = explode(" ;; ", $update);
        foreach($update as $upq){
            if (!empty($upq) ) {
                $this->db->query($upq);
            }
        }
    } else {
        echo $result;
    }

    echo date("H:i:s A");

}

function post_data($qry){
    $qry = htmlspecialchars(urlencode($qry));
    $data = "qry=" . $qry;
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL,$this->config->item("sync_server_url") );
    curl_setopt( $ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt ( $ch, CURLOPT_HTTPHEADER, array ( 'Content-length: ' . strlen($qry) ) );
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt( $ch, CURLOPT_POST,1);
    curl_setopt( $ch, CURLOPT_POSTFIELDS,$data);
    curl_setopt( $ch, CURLOPT_CRLF, 1);
    $result = curl_exec($ch);
    curl_close($ch);
    if ( 1 === intval($result) ) {
        return TRUE;
    } else {
        echo $result;
        return $result;
    }
}}?>

after i created code for server:sync_server.php 在我为server:sync_server.php创建代码后

<?php class Sync_server extends CI_Controller {
function __construct() {
    parent::__construct();
    $this->load->database();    
}

function index() {
    if (isset( $_POST['qry'] ) ) {
        $qry = htmlspecialchars_decode( urldecode( $_POST['qry']));


        //$this->connect_local_db();

        $qry = explode(" ;; ", $qry);


        foreach($qry as $q) {

            $q = explode( "||", $q );




            $exists = $this->db->query( $q[0] ) or die ("<hr/>" . mysqli_error() ."<br/>" . $q[0]);


            if ( $exists && mysqli_num_rows( $exists ) ) {
                $this->db->query( $q[2] ) or die ("<hr/>" . mysqli_error()."<br />".$q[2]);
            } else{
               $this->db->query( $q[1] ) or die ("<hr/>" . mysqli_error()."<br />".$q[1]);
            }

            //$this->db->query($q);
        }
        die("1");
    } else {
        echo "qry not found";
    }
} }?>

but i am getting an error in database 但是我在数据库中遇到错误 在此处输入图片说明

please, anyone, help me 请任何人帮我

inside your config file define your local database and server database. 在配置文件中定义本地数据库和服务器数据库。 like this.i hope it will help you. 像这样。希望对您有帮助。

//database.php //database.php

$active_group = 'default';
$query_builder = TRUE;

$db['default'] = array(
    'dsn'   => '',
//  'hostname' => 'localhost',
//  'username' => 'root',
//  'password' => 'root',
//  'database' => 'alpaca_web',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);
switch (ENVIRONMENT) {
    case 'development':
        $db['default']['hostname'] = 'localhost';
        $db['default']['username'] = 'user_name';
        $db['default']['password'] = 'root';
        $db['default']['database'] = 'database_name';
        break;
    case 'testing':
    case 'production':
        $db['default']['hostname'] = 'localhost';
        $db['default']['username'] = 'user_name';
        $db['default']['password'] = 'password';
        $db['default']['database'] = 'database_name';
        break;
    default:
        $db['default']['hostname'] = 'localhost';
        $db['default']['username'] = 'user_name';
        $db['default']['password'] = 'password';
        $db['default']['database'] = 'database_name';
        break;
}

//config.php //config.php

switch (ENVIRONMENT) {
    case 'development':
        $config['base_url'] = 'http://localhost/site_name/';
        break;
    case 'testing':
        $config['base_url'] = 'http://server ip or name/';
        break;
    default:
        $config['base_url'] = 'http://localhost/site_name/';
        break;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM