Welcome Guest, Not a member yet? Register   Sign In
What's the equivalent of this PDO method in CI3
#1

(This post was last modified: 01-05-2023, 02:41 AM by demon_da.)

Hey Guys
I'm looking for the equivalent of this PDO method in CI3 query builder with Mysqli driver so i can retrieve a value without iterating through the whole object:

PHP Code:
$recs $GLOBALS['db']->prepare("SELECT * FROM tbl_settings WHERE set_name LIKE 'sys_%'");
$recs->execute();
$res $recs->fetchAll(PDO::FETCH_KEY_PAIR);
$sys_version $res['sys_version'];

Thanks in advance 
Reply
#2

See https://www.codeigniter.com/userguide3/d...milar-data
Reply
#3

(01-06-2023, 06:40 PM)kenjis Wrote: See https://www.codeigniter.com/userguide3/d...milar-data
Actually, i meant this part: IRPDO::FETCH_KEY_PAIR
Reply
#4

There is no way.
See https://www.codeigniter.com/userguide3/d...sults.html
Reply
#5

If you're looking for the equivalent of the PDO method in CodeIgniter 3's query builder with the MySQLi driver, you can achieve this by using the result_array() method.

Here's an example of how you can retrieve a value without iterating through the whole object:


$query = $this->db->select('*')->from('tbl_settings')->like('set_name', 'sys_')->get();
$results = $query->result_array();

$sys_version = '';
foreach ($results as $result) {
if ($result['set_name'] == 'sys_version') {
$sys_version = $result['set_value'];
break;
}
}
In this example, the select() method selects all columns from the tbl_settings table. The like() method filters the results to only include rows where the set_name column starts with 'sys_'.

The get() method executes the query and returns a result object. The result_array() method converts the result object into an array of associative arrays.

Finally, the foreach loop iterates over the array and checks if the set_name column equals 'sys_version'. If it does, the corresponding set_value is assigned to the $sys_version variable.

I hope this helps. Let me know if you have any further questions or concerns.
Reply
#6

You can achieve the same result in CodeIgniter 3 Query Builder with Mysqli driver using the following code:


$this->db->select('set_name, set_value');
$this->db->from('tbl_settings');
$this->db->like('set_name', 'sys_');
$query = $this->db->get();
$results = $query->result();

$sys_version = null;
foreach ($results as $result) {
if ($result->set_name == 'sys_version') {
$sys_version = $result->set_value;
break;
}
}
This code will retrieve all records from the tbl_settings table where set_name contains the string 'sys_' and return an array of objects containing the set_name and set_value columns. Then, it loops through the results to find the value for the 'sys_version' key and assigns it to the $sys_version variable.

Note that unlike the PDO method, this method requires you to loop through the results to find the desired value, as there is no built-in method to fetch key-value pairs in CodeIgniter 3 Query Builder with Mysqli driver.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB