Welcome Guest, Not a member yet? Register   Sign In
Active Record problem
#1

[eluser]zfgf[/eluser]
Hi all, I have a problem in using active record

I need to get a row count after deleted,so i write this code:

$this->db->where('id',1);
$this->db->delete('test');

$this->db->where('id <',100);
$this->db->from('test');
$this->db->count_all_results();

but the sql output is : select count(*) from test where id = 1 and id < 100
,that i will never get the correct answer.

Any suggestions?
#2

[eluser]smilie[/eluser]
Hm, that should not have happend :-)

A 'quick dirty' trick would be:

Code:
$this->db->where(‘id’,1);
$this->db->delete(‘test’);

$db2 = $this->load->database('default',TRUE);
$db2->where(‘id <’,100);
$db2->from(‘test’);
$db2->count_all_results();

Where 'default' is the name of the DB in your config file;
This way, you will create another DB instance which should not take
$this->db->where(‘id’,1);
in the query.

But, imho - once delete() has been executed, the mysql should 'clean' it up... I just looked in the manual, but could not find anything regarding this.

Cheers,
Smilie
#3

[eluser]zfgf[/eluser]
Hi Smilie

thank you very much~

i can't find anything useful in manual too,and i think the Delete() should execute after $this->db->delete(),but in fact,when db->count_all_results(),it return '1'.It seems that the delete() has been executed after count_all_results().

the execute order like this:

where: id = 1.
where: id = 1 and id < 100.
count_all_results.
delete.

just i don't know why...

thank you again~
#4

[eluser]smilie[/eluser]
Hi,

I think the problem is more the fact that for some reason $this->db->where('id','1') is not set 'free' after delete was executed and therefore also included in the count. But, the question is why Smile

Anyway, hope that my workaround has helped Smile

Cheers,
Smilie
#5

[eluser]zfgf[/eluser]
Hi smilie

i think your suggestion would work, thank you

but i don't know why the 'where' is not clean,and the 'delete' execute after the 'count_all_results'...
#6

[eluser]InsiteFX[/eluser]
The database saves your queries!

Try this
Code:
$this->db->save_queries    = FALSE;

// then set it back
$this-db->save_queries    = TRUE;

InsiteFX
#7

[eluser]zfgf[/eluser]
[quote author="InsiteFX" date="1293467484"]The database saves your queries!

Try this
Code:
$this->db->save_queries    = FALSE;

// then set it back
$this-db->save_queries    = TRUE;

InsiteFX[/quote]

THX!




Theme © iAndrew 2016 - Forum software by © MyBB