What's the equivalent of this PDO method in CI3 |
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_%'"); (01-06-2023, 06:40 PM)kenjis Wrote: See https://www.codeigniter.com/userguide3/d...milar-dataActually, i meant this part: IRPDO::FETCH_KEY_PAIR
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.
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. |
Welcome Guest, Not a member yet? Register Sign In |