我在正常工作状态下创建了PHP应用,客户端希望该应用在本地主机上没有Internet且互联网自动连接到实时服务器的情况下工作,我在搜索google,但没有从本地到服务器的任何同步数据库代码显示了该代码,用于localhost
controller: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;
}
}}?>
我为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";
}
} }?>
请任何人帮助我
答案 0 :(得分:0)
定义本地数据库和服务器数据库。像这样。希望对您有帮助。
// 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
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;