Codeigniter – multiple database connections

You should provide the second database information in `application/config/database.php´ Normally, you would set the default database group, like so: $db[‘default’][‘hostname’] = “localhost”; $db[‘default’][‘username’] = “root”; $db[‘default’][‘password’] = “”; $db[‘default’][‘database’] = “database_name”; $db[‘default’][‘dbdriver’] = “mysql”; $db[‘default’][‘dbprefix’] = “”; $db[‘default’][‘pconnect’] = TRUE; $db[‘default’][‘db_debug’] = FALSE; $db[‘default’][‘cache_on’] = FALSE; $db[‘default’][‘cachedir’] = “”; $db[‘default’][‘char_set’] = “utf8”; $db[‘default’][‘dbcollat’] = “utf8_general_ci”; … Read more

How to add an ORDER BY clause using CodeIgniter’s Active Record methods?

I believe the get() function immediately runs the select query and does not accept ORDER BY conditions as parameters. I think you’ll need to separately declare the conditions, then run the query. Give this a try: $this->db->from($this->table_name); $this->db->order_by(“name”, “asc”); $query = $this->db->get(); return $query->result(); CodeIgniter Documentation order_by()

What is a slug?

A slug is a part of the URL when you are accessing a resource. Say you have a URL, such as the one below, that displays all of the cars in your system: http://localhost/cars When you would want to reference a particular car in your system, you would provide the following URL: http://localhost/cars/audi-a6/ Notice how … Read more

Destroying a specific session in Code Igniter

Create a new column in the ci_session table. ALTER TABLE `yourdatabase`.`ci_sessions` ADD COLUMN `userid` VARCHAR(45) NULL AFTER `user_data` ; Then in your login function get the id from your login process and before adding the userdata information to the session do: /* Delete any existing sessions with the current userid session. Note – a new … Read more

Advantages / Disadvantages of pconnect option in CodeIgniter

Just look up general best practices for persistent connections. My suggestions. By default, DO NOT If you have: Dedicated web server and database hardware in production and have tuned the web server and database correctly and have an accurate production-like test environment And still think your performance problems are caused by database connection time, CONSIDER … Read more

Redirect with CodeIgniter

redirect() URL Helper The redirect statement in code igniter sends the user to the specified web page using a redirect header statement. This statement resides in the URL helper which is loaded in the following way: $this->load->helper(‘url’); The redirect function loads a local URI specified in the first parameter of the function call and built … Read more

base_url() function not working in codeigniter

In order to use base_url(), you must first have the URL Helper loaded. This can be done either in application/config/autoload.php (on or around line 67): $autoload[‘helper’] = array(‘url’); Or, manually: $this->load->helper(‘url’); Once it’s loaded, be sure to keep in mind that base_url() doesn’t implicitly print or echo out anything, rather it returns the value to … Read more

subquery in codeigniter active record

->where() support passing any string to it and it will use it in the query. You can try using this: $this->db->select(‘*’)->from(‘certs’); $this->db->where(‘`id` NOT IN (SELECT `id_cer` FROM `revokace`)’, NULL, FALSE); The ,NULL,FALSE in the where() tells CodeIgniter not to escape the query, which may mess it up. UPDATE: You can also check out the subquery … Read more

Header and footer in CodeIgniter

Here’s what I do: <?php /** * /application/core/MY_Loader.php * */ class MY_Loader extends CI_Loader { public function template($template_name, $vars = array(), $return = FALSE) { $content = $this->view(‘templates/header’, $vars, $return); $content .= $this->view($template_name, $vars, $return); $content .= $this->view(‘templates/footer’, $vars, $return); if ($return) { return $content; } } } For CI 3.x: class MY_Loader extends CI_Loader … Read more