3

In my controller I have:

$query = $this->db->query("select * from table");

I want to have a file myquery.sql in which the only contents of that file are:

select * from table

And so I can do something like:

$query = $this->db->query(myquery.sql);

What is the right syntax to do this?

1
  • As for why, the query I'm using is 700 lines and I didn't want it to make my otherwise concise controller difficult to navigate through. I wanted to use a stored procedure but couldn't get it to work and read somewhere that stored procedures don't play nice with Code Igniter, but maybe one day I'll try again. Commented Jul 19, 2016 at 2:32

1 Answer 1

2

You should be able to achieve that using:

$query = $this->db->query(file_get_contents('myquery.sql'));

Read more about file_get_contents.


You might need to use the correct path to myquery.sql, but that's the basic idea.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks guys, that got it to work. It literally required the full path all the way down to the drive letter though, which is annoying...why can't I just use the filename like I can with an include statement?
@DavidBrooks TBH, I don't know how to answer that path problem. Maybe codeigniter is messing things up, but I really don't know.

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.