Welcome Guest, Not a member yet? Register   Sign In
How to execute a prepared statement in CI
#1

Hi all, I have been unable to find documentation on this. In MySQL, I have the following prepared statement:

Code:
SET @sql = NULL;

SELECT
GROUP_CONCAT(DISTINCT'MAX(IF(Name = ''', Name, ''', Name, NULL)) AS `', Name, '`') INTO @sql
from Contact_Types;

SET @sql = CONCAT('SELECT c.First_Name, ', @sql, ' from Contacts c
left join Contacts_Contact_Types cct
  on c.Contact_Id = cct.Contact_Id
left join Contact_Types ct
  on cct.Contact_Type_Id = ct.Contact_Type_Id
group by c.Contact_Id');

PREPARE statement FROM @sql;
EXECUTE statement;
DEALLOCATE PREPARE statement;

This allows me to dynamically retrieve "contact types" associated with a contact. How can I execute this statement in CI and retrieve the results?

I have tried the following and receive a SQL syntax error upon execution:

Code:
$this->db->query("PREPARE statement FROM @sql; EXECUTE statement; DEALLOCATE PREPARE statement;");

Thanks for any help you can give!
Reply
#2

CI doesn't support prepared statements. You can use bindings vor replacement like described on the bottom of page "Query Bindings":

https://ellislab.com/codeigniter/user-gu...eries.html

For safe querys you should use the active record class:

https://ellislab.com/codeigniter/user-gu...ecord.html

Reply




Theme © iAndrew 2016 - Forum software by © MyBB