Gary the Llama
Gary the Llama

Reputation: 321

Query using CodeIgniter's Active Record not returning a value…

Here's the code I'm running.

$query = $this->db->query("DECLARE @tmp TABLE (ContactID UNIQUEIDENTIFIER)
INSERT INTO tblCD (" . ArrayToString($fieldsCDArray) . ")
OUTPUT Inserted.ContactID INTO @tmp
VALUES (" . ArrayToDBString($valuesCDArray) . ")
INSERT INTO tblCCO (ContactID, " . ArrayToString($fieldsCCOArray) . ")
VALUES ((SELECT ContactID FROM @tmp), " . ArrayToDBString($valuesCCOArray) . ")
SELECT ContactID FROM @tmp");

$result = $query->row(); 

I've ran $this->db->last_query() to verify that the SQL code actually works in SQL server. It ends up returning a single row, as expected. But I get nothing returned from CodeIgniter with the above code. I've also tried running it with result() and result_array() just for kicks, and still nothing.

Does CodeIgniter not like returning results from a query with more than one statement in it or do I have to get the result a different way? (For anyone asking, I’m just trying to get the last inserted id but it's a GUID so I can't use $this->db->insert_id().

Any help or suggestions would be appreciated. Thanks.

Also, ignore the ArrayToDBString() methods, those are definitely not the issues since, like I said, I've printed the last query and ran it through SQL successfully. Also, the result I get when running it through SQL is a single column called ContactID.

Upvotes: 2

Views: 2091

Answers (1)

No Results Found
No Results Found

Reputation: 102774

I'm echoing $result and it's showing nothing

row() returns an object, not a string, even though you have selected only one column. To access the value, you still have to call it explicitly:

$record = $query->row();
echo $record->ContactID;

If that still doesn't work, try running your queries one at a time instead of passing all of them to $this->db->query() at once.

...and as always, turn error reporting on with error_reporting(E_ALL) and use something like var_dump() or print_r() to debug variables, instead of echo.

Upvotes: 1

Related Questions