看看#111和#116行
<?php
class Connection_model extends CI_Model {
private $validated;
private $sender_db;
private $sender_host;
private $sender_user;
private $sender_pw;
private $receiver_db;
private $receiver_host;
private $receiver_user;
private $receiver_pw;
public $err_sender;
public $err_receiver;
private function define_database($target) {
if (in_array('sender', $target)) {
$db['sender'] = array(
'dsn' => '',
'hostname' => $this->sender_host,
'username' => $this->sender_user,
'password' => $this->sender_pw,
'database' => $this->sender_db,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
return $db['sender'];
}
elseif (in_array('receiver', $target)) {
$db['receiver'] = array(
'dsn' => '',
'hostname' => $this->receiver_host,
'username' => $this->receiver_user,
'password' => $this->receiver_pw,
'database' => $this->receiver_db,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
return $db['receiver'];
}
}
// Validate the connection(s)
private function validate($target) {
/* @param: Can be 'sender' (string), 'receiver' (string) or 'sender' and 'receiver' (array) */
// Go through all parameters and define an array
if ($target == 'sender' || in_array('sender', $target)) {
$sessions = array('connection', 'sender_db', 'sender_host', 'sender_user', 'sender_pw');
}
elseif ($target == 'receiver' || in_array('receiver', $target)) {
$sessions = array('connection', 'receiver_db', 'receiver_host', 'receiver_user', 'receiver_pw');
}
else {
echo 'Error: illegal parameter. Please use sender or receiver instead.';
}
// Check if all keys from the array are saved in session
if (isset($sessions)) :
foreach ($sessions as $value) {
if (key_exists($value, $this->session->get_userdata())) {
$this->validated = true;
}
}
endif;
}
// Establish one or many connections
/* @param: Can be 'sender' (string), 'receiver' (string) or 'sender' and 'receiver' (array) */
public function establish($target) {
if ($target == 'receiver' || in_array('receiver', $target)) { $receiver = 1; }
elseif ($target == 'sender' || in_array('sender', $target)) { $sender = 1; }
if ($sender = 1 || $receiver == 1) {
$db_values = array();
$this->validate($target);
if ($this->validated) {
// Aight, let's go ahead and connect this baby
if ($sender == 1) {
array_push($db_values, 'sender');
$this->sender_db = $this->session->userdata('sender_db');
$this->sender_host = $this->session->userdata('sender_host');
$this->sender_user = $this->session->userdata('sender_user');
$this->sender_pw = $this->session->userdata('sender_pw');
if ($this->load->database($this->define_database($db_values))) {
$this->err_sender = 0;
return $this->load->database($this->define_database($db_values), TRUE);
}
else {
echo '<br>AHHHHHHHHHHHHHHHHH<br>';
$this->err_sender = 1;
$this->session->unset_userdata('connection');
}
}
elseif ($receiver == 1) {
array_push($db_values, 'receiver');
$this->receiver_db = $this->session->userdata('receiver_db');
$this->receiver_host = $this->session->userdata('receiver_host');
$this->receiver_user = $this->session->userdata('receiver_user');
$this->receiver_pw = $this->session->userdata('receiver_pw');
if ($this->load->database($this->define_database($db_values))) {
$this->err_receiver = 0;
return $this->load->database($this->define_database($db_values), TRUE);
}
else {
$this->err_receiver = 1;
$this->session->unset_userdata('connection');
}
}
else {
echo 'Error: illegal parameter. Please use sender or receiver instead.';
}
if ($this->err_receiver == 1 || $this->err_sender == 1) {
// redirect('home');
exit;
}
}
else {
echo 'Oops, there is an error! For some reason the property "validated" is not returning true (Connection_model.php)';
exit;
}
}
else {
echo 'Error: illegal parameter. Please use sender or receiver instead.';
}
}
}
我总是得到echo 'AHHHHHHHHHHHHHHHHH'
。这可能是因为if ($this->load->database($this->define_database($db_values)))
返回false
。但为什么它会返回false
?它只应在无法建立数据库连接时返回false
。
答案 0 :(得分:1)
我看到的第一个问题是,在您的方法establish()
中,您在if条件中有语法错误,因为您可能已经知道=
指定了变量的含义,{{1}比较两个变量。更改以下内容:
==
它应该是:
if ($sender = 1 || $receiver == 1) {
我看到第二个问题,当您在会话中检查密钥时。根据你的评论,你正在迭代会话数组来检查所有键是否被保存,如果设置了一个然后它将设置为true,然后检查下一个,但是如果不是设置你没有将它设置为false。修复如下:
if ($sender == 1 || $receiver == 1) {
第三个问题,当你检查你的数据库是否已经加载时,你的if语句没有检查bool因此总是给你相同的结果。要接收bool,您必须先将数据库if (isset($sessions)):
foreach ($sessions as $value) {
if (key_exists($value, $this->session->get_userdata())) {
$this->validated = true;
} else {
$this->validated = false;
break;
}
}
endif;
更改为db_debug
,然后尝试FALSE
数据库并检查结果如下:
initialiaze
现在在private function define_database($target) {
if (in_array('sender', $target)) {
$db['sender'] = array(
'dsn' => '',
'hostname' => $this->sender_host,
'username' => $this->sender_user,
'password' => $this->sender_pw,
'database' => $this->sender_db,
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => FALSE, //SET THIS TO FALSE
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE,
);
return $db['sender'];
} //....your other code
}
:
establish()
答案 1 :(得分:-1)
更改此行
if ($sender = 1 || $receiver == 1) {
到这一行
if ($sender == 1 || $receiver == 1) {