MySQL and GROUP_CONCAT() maximum length

前端 未结 7 978
难免孤独
难免孤独 2020-11-22 15:24

I\'m using GROUP_CONCAT() in a MySQL query to convert multiple rows into a single string. However, the maximum length of the result of this function is 10

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-22 15:33

    The short answer: the setting needs to be setup when the connection to the MySQL server is established. For example, if using MYSQLi / PHP, it will look something like this:

    $ myConn = mysqli_init(); 
    $ myConn->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');
    

    Therefore, if you are using a home-brewed framework, well, you need to look for the place in the code when the connection is establish and provide a sensible value.

    I am still using Codeigniter 3 on 2020, so in this framework, the code to add is in the application/system/database/drivers/mysqli/mysqli_driver.php, the function is named db_connect();

    public function db_connect($persistent = FALSE)
        {
            // Do we have a socket path?
            if ($this->hostname[0] === '/')
            {
                $hostname = NULL;
                $port = NULL;
                $socket = $this->hostname;
            }
            else
            {
                $hostname = ($persistent === TRUE)
                    ? 'p:'.$this->hostname : $this->hostname;
                $port = empty($this->port) ? NULL : $this->port;
                $socket = NULL;
            }
    
            $client_flags = ($this->compress === TRUE) ? MYSQLI_CLIENT_COMPRESS : 0;
            $this->_mysqli = mysqli_init();
    
            $this->_mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 10);
            $this->_mysqli->options(MYSQLI_INIT_COMMAND, 'SET SESSION group_concat_max_len = 1000000');
    
    ...
        }
    

提交回复
热议问题