How to set mysqli_options in Codeigniter?

We will explore the process of setting mysqli_options in CodeIgniter, providing you with the necessary insights to enhance the performance and functionality of your database interactions.

Accessing CodeIgniter's Database Instance:

`$db = (array)get_instance()->db;` This line retrieves the CodeIgniter database instance and casts it to an array, allowing access to its configuration parameters.

Creating a MySQLi Connection:

`$conn = mysqli_connect($db['hostname'], $db['username'], $db['password'], $db['database']);` A MySQLi connection is established using the database configuration parameters obtained from CodeIgniter.

Setting mysqli_options:

`mysqli_options($conn, MYSQLI_OPT_LOCAL_INFILE, true);` Here, we set a specific MySQLi option, `MYSQLI_OPT_LOCAL_INFILE`, to true. This option enables the use of the `LOAD DATA LOCAL INFILE` SQL statement, which can be beneficial for bulk data imports.

Setting mysqli_options in CodeIgniter

Let's delve into an example of how to set mysqli_options within the CodeIgniter framework:


<?php
// Accessing the CodeIgniter database instance
$db = (array)get_instance()->db;

// Creating a MySQLi connection
$conn mysqli_connect($db['hostname'], $db['username'], $db['password'], $db['database']);

// Setting mysqli_options
mysqli_options($connMYSQLI_OPT_LOCAL_INFILEtrue);

// Further database interactions can be performed using the $conn object
?>

Share