Codeigniter db->update() VS MySQL native UPDATE Affected rows: 0 - mysql

Using MySQL alone - If I make a basic update to a table like this:
UPDATE `SOMETABLE` SET `NAME` = 'John' WHERE `ID` = 1;
And the value of NAME = 'John' was already 'John' - in other-words - nothing is new, nothing to update. MySQL returns "Affected rows: 0 (Query took 0.0007 sec)"
If I make the same call - now using CodeIgniter - and then retrieve the affected rows like this:
$data = array(
'NAME' => 'John'
);
$this->db->where('ID', 1);
$this->db->update('SOMETABLE', $data);
$affect = $this->db->affected_rows();
echo $affect; // $affect echos 1
$affect ends up equaling 1.
I haven't got a problem with this - I just expected that if there was nothing to update - that codeigniter would behave the same way as MySQL and not edit something that does not need to be updated, and return 0 for affected_rows().
Have I got this wrong some way?
Is codeigniter overwriting 'John'? or not?

Try getting the query that CodeIgniter is running using the following code:
$this->db->last_query();
Also post the query you are using to interact with MySQL, just to confirm that the exact same query is being run.
CodeIgniter does have a hack for MySQL that adjusts the reporting of affected rows, however I was under the impression it was only for DELETE queries. If you look at system/database/drivers/mysql/mysql_driver.php or system/database/drivers/mysqli/mysqli_driver.php (whichever driver you are using and look at the variable var $delete_hack = TRUE;. Adjusting that might impact your result, could be worth a try?

Related

Laravel update query (Add a value to existing a value)

I am trying to update a record using Laravel.
I have gone through lot of StackOverflow Questions to check whether this question is already raised.
mysql query : UPDATE students SET total_marks = total_marks + 80 WHERE id = 1
I have to translate this mysql query into Laravel query builder, but couldn't get a solution yet.
Instead of getting the early value from DB before update, Can we update the table with one update query using Laravel Query Builder.
2 Queries way:
$student_marks = Students::select('total_marks')->where('id','=',1);
$current_mark = $student_marks['total_marks']+80;
$update_student_marks = Students::where('id','=',1)->update('total_marks','=',$current_mark);
How to update a record like this with single query builder in Laravel.
I think you need to make a few adjustments to your query.
First, you need to select the student correctly and than use Eloquent to call save method on it after setting the property to the correct value. I assume you are on Laravel 6.
$student_marks = Students::find($id);
$student_marks->total_marks += 80;
$student_marks->save();
Please, take a look at Laravel docs:
https://laravel.com/docs/6.x/eloquent
The reading takes some time but its definitely worth it. You will learn how to deal with eloquent and make your code better by using the most appropriate techniques.
You can use the save function for this.
$student_marks = Students::select('total_marks')->where('id','=',1);
$student_marks->total_marks += 80; //or $student_marks->total_marks = $student_marks->total_marks + 80;
$student_marks->save();
Pass update data as array
Try this way
$update = array('total_marks'=>$current_mark);
$update_student_marks = Students::where('id','=',1)->update($update);

Symfony 2 - Doctrine ORM Update query not working

I am trying to update a mysql table with following query using Doctrine. But the table is not get updated. Also below code didnt throw any error. I am totally confused. If i run the query, taken from getDQL() method, in mysql directly it showing 0 rows updated becuase of inproper qoutes. it is working after placed proper qoutes for values in the query. Need help to solve this puzzle.
Since i am new to doctrine, i will use the examples give in querybuilder class file.
$support = $this->createQueryBuilder('p')
->update('gcns', 'g')
->set("g.isActive", "0")
->andWhere("g.issn='".$issn."'");
Do you ever execute the query or are you just building it? You should have something along these lines to execute it:
$support->getQuery()->getSingleScalarResult();
If i run the query, taken from getDQL() method, in mysql directly it showing 0 rows updated becuase of inproper qoutes.
getDQL() returns DQL not SQL, so it will have improper quotesif you try to run it directly inside MySQL, but that's expected.
You shouldn't concatenate $issn into your query. You should use parameters instead:
$qb = $this->createQueryBuilder()
$support = $qb->update('gcns', 'g')
->set('g.isActive', '0')
->andWhere( $qb->expr()->eq('g.issn', ':issn') )
->setParameter( 'issn', $issn )
->getQuery()->getSingleScalarResult()
;

How to run 'SELECT FOR UPDATE' in Laravel 3 / MySQL

I am trying to execute SELECT ... FOR UPDATE query using Laravel 3:
SELECT * from projects where id = 1 FOR UPDATE;
UPDATE projects SET money = money + 10 where id = 1;
I have tried several things for several hours now:
DB::connection()->pdo->exec($query);
and
DB::query($query)
I have also tried adding START TRANSACTION; ... COMMIT; to the query
and I tried to separate the SELECT from the UPDATE in two different parts like this:
DB::query($select);
DB::query($update);
Sometimes I get 0 rows affected, sometimes I get an error like this one:
SQLSTATE[HY000]: General error: 2014 Cannot execute queries while other unbuffered queries are active. Consider using PDOStatement::fetchAll(). Alternatively, if your code is only ever going to run against mysql, you may enable query buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.
SQL: UPDATE `sessions` SET `last_activity` = ?, `data` = ? WHERE `id` = ?
I want to lock the row in order to update sensitive data, using Laravel's database connection.
Thanks.
In case all you need to do is increase money by 10, you don't need to lock the row before update. Simply executing the update query will do the job. The SELECT query will only slow down your script and doesn't help in this case.
UPDATE projects SET money = money + 10 where id = 1;
I would use diferent queries for sure, so you can have control on what you are doing.
I would use a transaction.
If we read this simple explanations, pdo transactions are quite straightforward. They give us this simple but complete example, that ilustrates how everithing is as we should expect (consider $db to be your DB::connection()->pdo).
try {
$db->beginTransaction();
$db->exec("SOME QUERY");
$stmt = $db->prepare("SOME OTHER QUERY?");
$stmt->execute(array($value));
$stmt = $db->prepare("YET ANOTHER QUERY??");
$stmt->execute(array($value2, $value3));
$db->commit();
}
catch(PDOException $ex) {
//Something went wrong rollback!
$db->rollBack();
echo $ex->getMessage();
}
Lets go to your real statements. For the first of them, the SELECT ..., i wouldn't use exec, but query, since as stated here
PDO::exec() does not return results from a SELECT statement. For a
SELECT statement that you only need to issue once during your program,
consider issuing PDO::query(). For a statement that you need to issue
multiple times, prepare a PDOStatement object with PDO::prepare() and
issue the statement with PDOStatement::execute().
And assign its result to some temp variable like
$result= $db->query ($select);
After this execution, i would call $result->fetchAll(), or $result->closeCursor(), since as we can read here
If you do not fetch all of the data in a result set before issuing
your next call to PDO::query(), your call may fail. Call
PDOStatement::closeCursor() to release the database resources
associated with the PDOStatement object before issuing your next call
to PDO::query().
Then you can exec the update
$result= $db->exec($update);
And after all, just in case, i would call again $result->fetchAll(), or $result->closeCursor().
If the aim is
to lock the row in order to update sensitive data, using Laravel's database connection.
Maybe you can use PDO transactions :
DB::connection()->pdo->beginTransaction();
DB::connection()->pdo->commit();
DB::connection()->pdo->rollBack();

Can PDO rowCount() after UPDATE query show difference between "no changes made" and "unexisting row"?

I'm doing an update query with PDO. I would like to figure out if my update query did not change anything in the database, since:
the passed values are the same as already present in the database. I know that rowCount() in such a case returns 0.
the row I'm trying to update does not exist in the database. As far as I can see, rowCount()in such cases also returns 0.
Am I forced to precede my UPDATE by a SELECT statement, to figure out if the record I'm trying to update does in fact exist? Or is there another common practice for this sort of thing.
I've been perusing through the documentation, but cannot find a conclusive answer:
http://php.net/manual/en/pdostatement.rowcount.php
I've come across this StackOverflow answer, that suggests that rowCount() might return NULL in some scenario's, but I don't think it's apliccable to my scenario:
see Why does PDO rowCount() return 0 after UPDATE a table without modifying the existing data?
From the comments in this question:
If the data hasn't been modified, the rowCount will be zero. If the
data was modified, the rowCount will be one or higher. If there was an
error, rowCount will be null or false or something non-zero.
UPDATE
I've found another question that gives an example of the proposition in the comments below:
Getting the insert and update ID with PDO
UPDATE2
Another question proposes another solution, via PDO::MYSQL_ATTR_FOUND_ROWS
PDO - check if row was updated?
You could add conditionals to your 'where' clause so such as " and ColumnToUpdate <> 'NewValue'"
I've solved it using the suggestions of #hjpotter92.
// UID is the unique ID of my table, autoincremented etc...
// Firstly, let's try to update my row
$query = 'UPDATE my_table SET x=0, y=1, uid=LAST_INSERT_ID(uid) WHERE z=2';
$sth = $dbh->prepare($query);
if($sth->execute()) {
if($dbh->lastInsertId() == 0) { // Record was not found, so insert it.
$query = 'INSERT INTO my_table (x,y) VALUES (0,1)';
$sth = $dbh->prepare($query);
$sth->execute();
if($sth->rowCount() > 0) {
echo $dbh->lastInsertId(); // Return the UID of the inserted row
}
}
}

DBIx::Class update only one row

I am using DBIx::Class and I would like to only update one row in my table. Currently this is how I do it:
my $session = my_app->model("DB::Session")->find(1);
$session->update({done_yn=>'y',end_time=>\'NOW()'});
It works, but the problem is that when it does find to find the row, it does this whole query:
SELECT me.id, me.project_id, me.user_id, me.start_time, me.end_time, me.notes, me.done_yn FROM sessions me WHERE ( me.id = ? ): '8'
Which seems a bit much when all I want to do is update a row. Is there anyway to update a row without having to pull the whole row out of the database first? Something like this is what I am looking for:
my_app->model("DB::Session")->update({done_yn=>'y',end_time=>\'NOW()'},{id=>$id});
Where $id is the WHERE id=? part of the query. Does anyone know how to do this? Thanks!
You can run update on a restricted resultset which only matches this single row:
my_app->model("DB::Session")->search_rs({ id=> 1 })->update({done_yn=>'y',end_time=>\'NOW()'});
I suggest you use a DateTime->now object instead of literal SQL for updating the end_time column because it uses the apps servers date and time instead of the database servers and makes your schema more compatible with different RDBMSes.
Do you have a check if the row was found to prevent an error in case it wasn't?
You might want to use update_or_create instead.
You could use the "columns" attribute:
my $session = my_app->model("DB::Session")->find(1, {columns => "id"});