Part of the EllisLab Network
   
1 of 2
1
Incrementing a counter in the database
Posted: 31 August 2008 02:28 PM   [ Ignore ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  01-02-2007

I’m having a problem with a function that needs to update a counter for the records found.

It’s working fine on my local development machine but fails to update on the live server.

Can anyone give me an idea of where i’ve gone wrong.

Thanks,
Simon

function getAdvertiserEmailList ($region_id, $category_id)
    
{
        $this
->db->select('*');
        
$this->db->from('fq_company');
        
$this->db->join('fq_company_category', 'company_id = fq_company.id');
        
$this->db->where('region_id', $region_id);
        
$this->db->where('category_id', $category_id);
        
$query = $this->db->get();

        
$advertiser_id_array = array();
        
$output = "";
        
        
//Check if any companys are found
        
if ($query->num_rows() > 0) {

            
foreach ($query->result() as $advertisers) {
                
                    $advertiser_id_array[]
= $advertisers->id;
    
                    
$output .= $advertisers->company_email;
                    
$output .= ", ";
                
}
            
            $output
= substr ($output, 0, -2);
            
            
// Update company_count for emailed companies
            
$this->db->set('company_count', 'company_count + 1', FALSE);
            
$this->db->where_in('id', $advertiser_id_array);
            
$query = $this->db->update('fq_company');
        
        
}
        
        
return $output;
    
}
 Signature 

fastQuotes - South Africa’s quoting solution

Profile
 
 
Posted: 03 September 2008 01:38 AM   [ Ignore ]   [ # 1 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  01-02-2007

I am drawing a conclusion that this is a problem with MySQL 4.1.20 (local) v MySQL 5.0.45 (local). I am going to upgrade database on live server and see what happens.

 Signature 

fastQuotes - South Africa’s quoting solution

Profile
 
 
Posted: 03 September 2008 02:03 AM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

I’m doing the same thing on lower version mysql databases and it works fine. Maybe there is a problem getting all the ids you need in the $advertiser_id_array?

Profile
 
 
Posted: 03 September 2008 03:41 AM   [ Ignore ]   [ # 3 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  01-02-2007

Oops 4.1.20 is the live database. Ive tried so many ways of doing this and they all seem to work locally and not live.

This also works locally but not live:

if ($query->num_rows() > 0) {

            
foreach ($query->result() as $advertisers) {
                
                    $advertiser_id_array[]
= $advertisers->id;
    
                    
$output .= $advertisers->company_email;
                    
$output .= ", ";
                    
                    
$count = $advertisers->company_count;
                    
$count = $count + 1;
                    
                    
$sql = "UPDATE fq_company SET company_count = '$count' WHERE id = '$advertisers->id'";
                    
$this->db->query($sql);
                    
                
}
            
            $output
= substr ($output, 0, -2);
            
        
        
}
 Signature 

fastQuotes - South Africa’s quoting solution

Profile
 
 
Posted: 06 September 2008 10:20 AM   [ Ignore ]   [ # 4 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  01-02-2007

OK - so having upgrade the database on the live server I have confirmed that it’s not a database version issue. I have connected my localserver version to the live database and still doesn’t work so all I can think of it that it could be Windows vs Linux issue.  confused

 Signature 

fastQuotes - South Africa’s quoting solution

Profile
 
 
Posted: 07 September 2008 11:05 AM   [ Ignore ]   [ # 5 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  647
Joined  09-30-2006

Simon -

I just sent you an email. Try logging the queries to see what’s going on.

Cheers,
m

 Signature 

sitesquad.net | < insert catchy tagline here />

Profile
 
 
Posted: 08 September 2008 01:47 AM   [ Ignore ]   [ # 6 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  01-02-2007

Mirage,

Thanks for the suggestion - I was already trying to work out how to log database activity to the log files but hadn’t been able to work it out. Eventually on Saturday I found some info here http://codeigniter.com/user_guide/general/errors.html
that explains for to log anything into the log files using log_message.

As usual when you know how it’s easy with CodeIgniter!

Thanks again,
Simon

 Signature 

fastQuotes - South Africa’s quoting solution

Profile
 
 
Posted: 08 September 2008 02:44 AM   [ Ignore ]   [ # 7 ]  
Lab Assistant
Avatar
RankRank
Total Posts:  279
Joined  06-25-2007
CI Simon - 06 September 2008 10:20 AM

OK - so having upgrade the database on the live server I have confirmed that it’s not a database version issue. I have connected my localserver version to the live database and still doesn’t work so all I can think of it that it could be Windows vs Linux issue.  confused

i am not clear how is it be an operating system issue.

Mirage : would you please let me know what you suggest to resolve.

 Signature 

To be honest i know nothing but desire to learn something.

Profile
 
 
Posted: 08 September 2008 03:59 AM   [ Ignore ]   [ # 8 ]  
Grad Student
Avatar
Rank
Total Posts:  32
Joined  01-02-2007
Sumon - 08 September 2008 02:44 AM

i am not clear how is it be an operating system issue.

It wasn’t anything to do with the operating system. I still do not 100% understand what was happening but it was to do with the join in the original query and trying to use the ID field which was picking up the ID from the wrong table.

Why was it working locally - i need to do further tests but I think it was simply coincidence.

Of course I didn’t post enough details for anyone to pick this up red face but I learnt some stuff along the way.

 Signature 

fastQuotes - South Africa’s quoting solution

Profile
 
 
Posted: 29 May 2009 06:59 AM   [ Ignore ]   [ # 9 ]  
Summer Student
Total Posts:  5
Joined  05-29-2009

I have exactly same problem, and dont know how to solve it

  function increment_offence_count($message_id, $value)
  {
      $this->db->set(‘offence_count’, ‘offence_count + ‘.$value);
      $this->db->where(‘id’, $message_id);
      return $this->db->update($this->_table);
  }

if I use insert instead of update, it insert new row, but update didnt update anything. Does anybody knows what can be problem?

THank you

Profile
 
 
Posted: 29 May 2009 07:55 AM   [ Ignore ]   [ # 10 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2517
Joined  06-11-2007

Some advice that will solve every problem you ever have with running MySQL from PHP.

1.) Output the final query and see if it looks right.

ob_clean();
echo
$this->db->last_query();
exit();

2.) If it looks fine, try running it in phpMyAdmin or another GUI.

3.) If it runs fine, your PHP is not running the query correctly (don’t find this one happening so often).

4.) If it fails, you get an error message telling you what’s wrong. Then, correct your query.

Debugging my friend, is a wonderful thing.

 Signature 

Blog | Twitter | GitHub
————————-
CodeIgniter 2: Everything you need to know
————————
PyroCMS - open source modular CMS built with CodeIgniter
CleverAndy - get money for un-used concept designs
————————
Libraries: Asset, Dwoo, Cache, cURL, CLI, REST, Template

Profile
 
 
Posted: 29 May 2009 08:10 AM   [ Ignore ]   [ # 11 ]  
Summer Student
Total Posts:  5
Joined  05-29-2009
Phil Sturgeon - 29 May 2009 07:55 AM

Some advice that will solve every problem you ever have with running MySQL from PHP.

1.) Output the final query and see if it looks right.

ob_clean();
echo
$this->db->last_query();
exit();

2.) If it looks fine, try running it in phpMyAdmin or another GUI.

3.) If it runs fine, your PHP is not running the query correctly (don’t find this one happening so often).

4.) If it fails, you get an error message telling you what’s wrong. Then, correct your query.

Debugging my friend, is a wonderful thing.

Thanks for help, I’m newbie in Code Igniter, and didnt know how to write SQL statement from Active Record. I have to confess Im Senior Flex developer and I’m used to debug daily many hours smile but I’m not developing in PHP so much, this is just my sideproject smile

btw here is result:

UPDATE `wtu_messages` SET `offence_count` = ‘offence_count + 5’ WHERE `id` = ‘2’

problem is with difference quotes in SET part. It should be

UPDATE `wtu_messages` SET offence_count = offence_count + 5 WHERE `id` = ‘2’

But I don’t know how to write it via Active record, so I will rather try to create SQL statement as a string. Or maybe you know how to fix it via Active Record?

Profile
 
 
Posted: 29 May 2009 08:16 AM   [ Ignore ]   [ # 12 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2517
Joined  06-11-2007

There we go, that’ll be ActiveRecord trying to help again. Just do the following:

$this->db->set('company_count = company_count + 1');

Setting the 3rd param as false will stop XSS cleaning, but it will not stop the automatic quote marks for what it believes to be strings or values.

 Signature 

Blog | Twitter | GitHub
————————-
CodeIgniter 2: Everything you need to know
————————
PyroCMS - open source modular CMS built with CodeIgniter
CleverAndy - get money for un-used concept designs
————————
Libraries: Asset, Dwoo, Cache, cURL, CLI, REST, Template

Profile
 
 
Posted: 29 May 2009 08:21 AM   [ Ignore ]   [ # 13 ]  
Summer Student
Total Posts:  5
Joined  05-29-2009

this made SQL statement even worse

UPDATE `wtu_messages` SET `offence_count` = offence_count + 1 = ‘’ WHERE `id` = ‘10’

Profile
 
 
Posted: 29 May 2009 08:28 AM   [ Ignore ]   [ # 14 ]  
Summer Student
Total Posts:  5
Joined  05-29-2009

so, this works smile

$this->db->set(‘offence_count’,‘offence_count + ‘.$value, false); 

thanks for help again. I’m following you now on Twitter, I hope I will help you someday wink

Profile
 
 
Posted: 29 May 2009 08:32 AM   [ Ignore ]   [ # 15 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4839
Joined  07-14-2006

why not use the query method? then you have no problems with the single quotes.

Profile
 
 
   
1 of 2
1
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120464 Total Logged-in Users: 36
Total Topics: 126542 Total Anonymous Users: 3
Total Replies: 665376 Total Guests: 293
Total Posts: 791918    
Members ( View Memberlist )