简体   繁体   English

如果声明无法按预期工作

[英]If statement doesn't work as expected

Look at lines #111 and #116 看看#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.';
        }
    }

}

I ALWAYS get the echo 'AHHHHHHHHHHHHHHHHH' . 我总是得到echo 'AHHHHHHHHHHHHHHHHH' This is probably because if ($this->load->database($this->define_database($db_values))) returns false . 这可能是因为if ($this->load->database($this->define_database($db_values)))返回false But why does it return false ? 但为什么它会返回false It should only return false when the database connection could not be established. 它只应在无法建立数据库连接时返回false

The first problem I see, is in your method establish() , you have a syntax error in your if conditional, as you may already know = assigns what the variable means, and == compares two variables. 我看到的第一个问题是你的方法establish() ,你在if条件中有一个语法错误,你可能已经知道=分配变量意味着什么,并且==比较两个变量。 Change the following: 更改以下内容:

you have: 你有:

 if ($sender = 1 || $receiver == 1) {

it should be : 它应该是 :

 if ($sender == 1 || $receiver == 1) {

Second problem I see, when you check for the keys in session. 我看到的第二个问题,当您在会话中检查密钥时。 According to your comment, you're iterating through the sessions array to check if ALL keys are saved, if one is set then it will set to true, then check the next, but what if one is not set youre not setting it to false. 根据你的评论,你正在迭代会话数组以检查是否保存了所有键,如果设置了一个,那么它将设置为true,然后检查下一个,但是如果没有设置则你没有将它设置为false 。 Fix it like this: 修复如下:

if (isset($sessions)):
    foreach ($sessions as $value) {
        if (key_exists($value, $this->session->get_userdata())) {
            $this->validated = true;
        } else {
            $this->validated = false;
            break;
        }
    }
endif;

Third problem, when you're checking if your database has loaded, your if statement is not checking for a bool therefore always giving you the same result. 第三个问题,当你检查你的数据库是否已加载时,你的if语句没有检查bool因此总是给你相同的结果。 To receive a bool, you have to first change your database db_debug to FALSE , then try to initialiaze the db and check for results like this: 要接收bool,您必须先将数据库db_debug更改为FALSE ,然后尝试initialiaze db并检查结果如下:

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
}

Now in establish() : 现在在establish()

$db_obj = $this->load->database($this->define_database($db_values));
if ($db_obj->initialize()) {
    $this->err_sender = 0;
    return $db_obj;
} else {
    echo '<br>AHHHHHHHHHHHHHHHHH<br>';
    $this->err_sender = 1;
    $this->session->unset_userdata('connection');
}

change this line 改变这一行

if ($sender = 1 || $receiver == 1) {

to this line 到这条线

if ($sender == 1 || $receiver == 1) {

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

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