2

For a legacy project, I would like to execute this query using CodeIgniter :

DELIMITER $$
CREATE FUNCTION `getCustomerFullName`(intCustomerID INT) 
  RETURNS varchar(100) CHARSET latin1
  return CONCAT(
    (SELECT FirstName FROM Customer WHERE CustomerID = intCustomerID),
    ' ',
    (SELECT LastName FROM Customer WHERE CustomerID = intCustomerID))$$
DELIMITER ;

When I try to use $this->db->query(), I get this error :

You have an error in your SQL syntax; check the manual that corresponds to your 
MySQL server version for the right syntax to use near 
'DELIMITER $$ CREATE FUNCTION `getCustomerFullName`(intCustomerID INT) RETURNS '
at line 1

How can I execute such "multi-line" query using CodeIgniter ?

3 Answers 3

2

The issue is in the query, not CodeIgniter. Use the regular delimiter inside your function:

DELIMITER $$
CREATE FUNCTION `getCustomerFullName`(intCustomerID INT) 
  RETURNS varchar(100) CHARSET latin1
  return CONCAT(
    (SELECT FirstName FROM Customer WHERE CustomerID = intCustomerID),
    ' ',
    (SELECT LastName FROM Customer WHERE CustomerID = intCustomerID));
$$
DELIMITER ;
Sign up to request clarification or add additional context in comments.

1 Comment

it is possible to call a function in codeigniter sql query?
1

if you want to execute a Function or SP, you need to add this to mysql (in the mtop menu of your phpmyadmin or Workbench) you have many otions

in More (routine / new / add new)
create a function or sp

Then in CI you only call like

$this->db->query('call Function()');

1 Comment

No. I need to create the function IN CodeIgniter. I'm using its migration mechanism, and a lot of different instances. I cannot use PhpMyAdmin in that case, it would require too much maintenance.
0

Best solution i get was looking inside system libs and use it.

$this->db->simple_query("YOUR FULL SQL STRING");

I was looking on google and not solution provided.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.