4

I am using Laravel 5.2. I have a raw SQL statement that I like to execute.

Here is what I tried

DB::select( $sql );

However, this works for the first query. If I execute another query in the same run I get the following error

FatalErrorException in Connection.php line 323: Cannot access empty property

So if I execute this

DB::select( sql1 );

and then

DB::select( $sql2 );

How can I execute multiple queries in the same run time.

I will get an error. Do I need to reinitialize the DB class before I call the second query? if so how would I reset it?

UPDATED

dd( $sql  );

I get the following

 "  SELECT TOP 1 '1' FROM survey_answer_defined INNER JOIN survey_answer_groups ON survey_answer_groups.id = survey_answer_defined.group_id WHERE survey_answer_groups.interview_id = '243' AND survey_answer_groups.control_id IN(300) AND 'E' IN('A','B','D','E')"

If I try the following instead

DB::select(DB::unprepared($sql)) 

I get the following error

SQLSTATE[HY090]: [Microsoft][ODBC Driver Manager] Invalid string or buffer length (SQL: )

2 Answers 2

4

Can you write your sql queries inside a variable and pass it like the following:

\DB::unprepared( $mySQLQuery ); //mySQLQuery is complete query you want to be executed as you would do inside the DBMS..

Hope this helps. Cheers.

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

1 Comment

I am trying to select a data set. DB::raw or DB:unprepared will execute the query and will return true/false not a dataset. Additionally per my notes in the question. DB::select(DB::unprepared($sql)) will give me an error SQLSTATE[HY090]: [Microsoft][ODBC Driver Manager] Invalid string or buffer length (SQL: )
0

I figured it out.

I think selecting '1' does not work with Laravel.

Here is wha I did for the query to work

I changed

SELECT TOP 1 '1' 
FROM survey_answer_defined INNER JOIN survey_answer_groups ON survey_answer_groups.id = survey_answer_defined.group_id WHERE survey_answer_groups.interview_id = '243' AND survey_answer_groups.control_id IN(300) AND 'E' IN('A','B','D','E')

to

SELECT TOP 1 survey_answer_defined.id 
FROM survey_answer_defined INNER JOIN survey_answer_groups ON survey_answer_groups.id = survey_answer_defined.group_id 
WHERE survey_answer_groups.interview_id = '243' AND survey_answer_groups.control_id IN(300) AND 'E' IN('A','B','D','E')"

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.