EllisLab text mark
Advanced Search
     
update query
Posted: 14 November 2010 12:55 PM
Joined: 2010-11-11
10 posts

Hi,

I wish to make something like:

$query 'update table set field = value where id = 3';
$result $this->db->($query);
$count $result->num_rows(); //should return the number of rows affected by the last query
return $count

Instead, the only workaround i found is:

$query = ‘select * from table where id = 3’;
$result = $this->db->($query);
$count = $result->num_rows();
if($count > 0){
$query = ‘update table set field = value where id = 3’;
$result = $this->db->($query);
}
return $count;

does anyone know how i can use the first form?
Thanx

 
Posted: 14 November 2010 01:51 PM   [ # 1 ]   [ Rating: 0 ]
Joined: 2010-06-27
140 posts

i assume ‘id’ is the primary key? if so it should only affect 1 row if successful

function update_table($id$value)
{
  $this
->db->set('field'$value);
  
$this->db->where('id'$id);
  return 
$this->db->update('table'); // returns true if successful, false otherwise
 Signature 

“I Reject Your Reality and Substitute My Own” - Adam Savage, M5 Inc

 
Posted: 14 November 2010 01:55 PM   [ # 2 ]   [ Rating: 0 ]
Joined: 2007-05-02
17 posts
André Padez - 14 November 2010 05:55 PM

Hi,

I wish to make something like:

$query 'update table set field = value where id = 3';
$result $this->db->($query);
$count $result->num_rows(); //should return the number of rows affected by the last query
return $count

Instead, the only workaround i found is:

$query = ‘select * from table where id = 3’;
$result = $this->db->($query);
$count = $result->num_rows();
if($count > 0){
$query = ‘update table set field = value where id = 3’;
$result = $this->db->($query);
}
return $count;

does anyone know how i can use the first form?
Thanx


http://ellislab.com/codeigniter/user-guide/database/helpers.html

$query 'update table set field = value where id = 3';
$result $this->db->($query);
$count $result->affected_rows(); //should return the number of rows affected by the last query
return $count
 
Posted: 14 November 2010 02:12 PM   [ # 3 ]   [ Rating: 0 ]
Joined: 2010-11-11
10 posts

Thank you both.

@Rolly1971, when you say it returns true if successful, i don’t think it’s what i’m looking for; i presume it will return true even if it affects zero rows, right?

@Georgi Veznev, i believe that is exactly what i was looking for. that’s what i am going to use

 
Posted: 14 November 2010 02:21 PM   [ # 4 ]   [ Rating: 0 ]
Joined: 2010-11-11
10 posts

Oh, and @Georgi Veznev

it’s not

$count $result->affected_rows(); 

but

$count $this->db->affected_rows();