无法插入数据库

时间:2015-07-20 09:37:45

标签: php mysqli

<?php
    require_once '/var/www/html/es/vendor/autoload.php';

    class test {          

        //public $params = array();
        public $mysql_con;
        public $es_con; 

                public function __construct($es_username, $es_password, $server_name, $db_username, $db_password, $db_name) {
        $params = array();
            $params['connectionParams']['auth'] = array(
                    $es_username,
                    $es_password,
                    'Basic' 
            );

            $es_con = new Elasticsearch\Client($params);

            if ($es_con) {

                $mysql_con = mysqli_connect($server_name, $db_username, $db_password, $db_name);
                if (!$mysql_con) {
                        echo nl2br("Connected successfully to elasticsearch. Please check your credentials for database\n"); 
                }
                else {
                    echo nl2br("Connected successfully to both elasticsearch and database\n") ; 
                }           
            }
            else {
                echo nl2br("Failed to connect to elasticsearch. Please check your credentials\n");
            }

                }            

        public function insert() {

            $insert_query = "INSERT INTO user (username,firstname,lastname,profile_about) VALUES ('shreyas','shreyas','r','Let us get out !!')";
            $run_insert_query = mysqli_query($this->mysql_con, $insert_query);

            if ($run_insert_query) {

                $select_query = "SELECT * FROM user ORDER BY id DESC LIMIT 1";
                $run_select_query = mysqli_query($this->mysql_con, $select_query);

                while ($selected_row = mysqli_fetch_array($run_query)) {
                    $id = $selected_row['id'];
                    $username = $selected_row['username'];
                    $firstname = $selected_row['firstname'];
                    $lastname = $selected_row['lastname'];
                    $profile_about = $selected_row['profile_about'];
                }

                $es_insert = array();
                $es_insert['body']  = array('id' => $id, 'username' => $username, 'firstname' => $firstname, 'lastname' => $lastname, 'profile_about' =>  $profile_about);
                $es_insert['index'] = 'test';
                $es_insert['type']  = 'jdbc';
                $es_insert['id']    = $id;
                $check_insert = $this->es_con->index($params);

                if($check_insert) {
                    echo nl2br("Successfully inserted to both database and elasticsearch\n");
                }
            }
            else {
                echo nl2br("Failed to insert into database hence closing the connection\n");            
            }
        }

    }

    $connection = new test('pavan', 'password', 'localhost', 'root', 'password', 'sample');        

        $connection->insert();

?>

运行代码时出现以下错误 -

mysqli_query()期望参数1为mysqli,在第39行的/var/www/html/es/combined.php中给出null

有人可以帮我调试吗?我想将数据插入数据库以及elasticsearch。

1 个答案:

答案 0 :(得分:2)

您未在$mysql_con中将$this->mysql_con设置为__construct。这就是参数1 $this->mysql_connull的原因。

else {
    echo nl2br("Connected successfully to both elasticsearch and database\n"); 
    $this->mysql_con = $mysql_con; // Add this line
    $this->es_con = $es_con; // After OP's comment, add this line too
}