Welcome Guest, Not a member yet? Register   Sign In
update query
#1

[eluser]André Padez[/eluser]
Hi,

I wish to make something like:

Code:
$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:

Quote:$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
#2

[eluser]Rolly1971[/eluser]
i assume 'id' is the primary key? if so it should only affect 1 row if successful

Code:
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
}
#3

[eluser]Georgi Veznev[/eluser]
[quote author="André Padez" date="1289775347"]Hi,

I wish to make something like:

Code:
$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:

Quote:$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[/quote]


http://ellislab.com/codeigniter/user-gui...lpers.html

Code:
$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;
#4

[eluser]André Padez[/eluser]
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
#5

[eluser]André Padez[/eluser]
Oh, and @Georgi Veznev

it's not
Code:
$count = $result->affected_rows();
but
Code:
$count = $this->db->affected_rows();




Theme © iAndrew 2016 - Forum software by © MyBB