How to determine success of MySQL transaction - php

In a regular single query statement to MySQL, I can use mysql_affected_rows()==1 to determine whether or not something was updated. Now suppose I am trying to perform a transaction of the following:
USE myDB;
START TRANSACTION;
UPDATE members SET member_active=0 WHERE member_id = 53;
UPDATE member_subscriptions SET subscription_active=0 WHERE member_id = 53;
COMMIT;
ROLLBACK;
Since I have those two update statements, should I could on a successful transaction being equivalent to mysql_affected_rows()==2 ?
OR, is there a better way I should be checking for success?

You should hold the value of mysql_affected_rows in variables. And total updation will be some of those variables.

If you have track of both transactions and can have even better judgment which transaction is not performed and what would be the reaction.
USE myDB;
START TRANSACTION;
UPDATE members SET member_active=0 WHERE member_id = 53;
$count1= mysql_affected_rows();
UPDATE member_subscriptions SET subscription_active=0 WHERE member_id = 53;
$count2= mysql_affected_rows();
COMMIT;
ROLLBACK;
$total_affected_rows = $count1 + $count2;

Yes checking mysql_affected_rows()==2 is an alternative way from application level.
Its going to be 100% successful(commit) or nothing (rolled back) as you are using proper transactions.
EDIT: You can use ROW_COUNT() function in MySQL to get affected rows from last query as:
USE myDB;
START TRANSACTION;
UPDATE members SET member_active=0 WHERE member_id = 53;
SELECT ROW_COUNT() INTO #count1;
UPDATE member_subscriptions SET subscription_active=0 WHERE member_id = 53;
SELECT ROW_COUNT() INTO #count2;
COMMIT;
ROLLBACK;
then the sum of these should be equals to 2
SELECT ((#count1 + #count2) = 2) AS status;

Related

Check Whether Particular query from multiple query is affected or not

Let Suppose i have Following code which updates two different table
if(isset($_POST['submit'])){
$updateq = $conn->query("UPDATE `tbl1` SET `field1`= '$field1'");
$updater = $conn->query("UPDATE `tbl2` SET field2 ='$field2'");
//Here I want to check if first update query
//is affected any row or not
if(mysqli_affected_rows($conn) > 0){
....
}
}
So is it possible to check whether first query is updated any row or not ?
IN mysql
ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or
INSERT. For other statements, the value may not be meaningful.
so,
UPDATE `tbl1` SET `field1`= '$field1'
SET #countRow = ROW_COUNT();
if (#countRow>0) then
UPDATE `tbl2` SET field2 ='$field2'
end if;
you have to integrate this in php as it is purely mysql syntax

Mysqli num_rows check to slow?

i have an c++ program that sending POST of logs to my server and store it on database, the problem is that the checking of duplicates before insert a new row is not working, i think that the program send the POST very fast and there is no delay between the POSTS to the server so the Mysqli can't handle this, is there any solution from server client? maybe locking rows or something?
$date = date('Y-m-d', time());
$prep_select_qa = 'SELECT * from `logs` WHERE `guid` = ? AND `error_code` = ? AND `date_create` = ?';
$select_qa = $db->prepare($prep_select_qa);
$select_qa->bind_param('sss', $_POST['guid'], $_POST['error_code'], $date);
$select_qa->execute();
$select_qa->store_result();
$num_rows = $select_qa->num_rows;
if($num_rows == 0)
{
$prep_insert_qa = 'INSERT INTO `logs` (`type`, `guid`, `sent_by`, `class_and_method`, `api_method`, `error_code`, `error_text`, `date_create`) VALUES (?,?,?,?,?,?,?,?)';
$insert_qa = $db->prepare($prep_insert_qa);
$insert_qa->bind_param('ssssssss', $new, $_POST['guid'], $_POST['sentBy'], $_POST['classAndMethodName'], $_POST['APImethod'], $_POST['ErrorCode'], $_POST['ErrorText'], $date);
$insert_qa->execute();
$insert_qa->store_result();
}
First, the answer to your question is that you are retrieving all the rows in order to count them. Presumably, this requires reading all the data in the table and returning some of it (unless you have indexes). A faster method is to check the value returned by this query:
SELECT count(*)
FROM `logs`
WHERE `guid` = ? AND `error_code` = ? AND `date_create` = ?';
And an even faster method is not to count but to determine if any row exists:
SELECT EXISTS (SELECT 1
FROM `logs`
WHERE `guid` = ? AND `error_code` = ? AND `date_create` = ?'
)
This will return 1 if the row exists and 0 otherwise. Both of the above queries and your original query will benefit from having an index on guid, error_code, date_create.
In practice, you should follow Marvin's advice and use a unique index. This means the database does the checking via a unique index rather than the application. One very important reason is a race condition. If two users are inserting the same row at the same time, both might execute the if statement, find there are no matching rows in the table, and then insert duplicate rows.
The SELECT scheme must be enclosed in a BEGIN...COMMIT transaction and have FOR UPDATE on it. Otherwise, some other connection can slip in and defeat your check.
Instead, try to do it in a single, atomic, instruction:
Once you have an INDEX that will prevent duplicates...
INSERT IGNORE -- Silently does nothing if it is a dup.
INSERT...ON DUPLICATE KEY UPDATE -- Lets you change something as you try to insert a dup.
Also, the INSERT solutions will be faster (which was your original question).

How to determine if MySQL UPDATE actually matched any rows with WHERE clause?

I want to run an update like such in PHP
// pseudocode
UPDATE invoice SET due_date=? WHERE invoice_id=? AND creater_id=?;
IF AFFECTED_ROWS == 1 THEN
UPDATE invoice_item SET price=? WHERE invoice_id=?
For added security, I appended creater_id to ensure that the code only updates if the logged in user is the invoice creator, otherwise, the system will not update.
I originally intended to check this condition using AFFECTED_ROWS. But eventually after much frustration, I realise AFFECTED_ROWS return 0 if all the new values are the same as the old values. This means that even if I have different values for the invoice_item, they will not be updated.
Other than doing a SELECT before the UPDATE, is there SQL query or PHP functions that will tell me if the UPDATE matched any row, so that I can proceeed to UPDATE invoice_item accordingly?
You can use ROW_COUNT() and if you read that it explains that when connecting to the DB you can specify the CLIENT_FOUND_ROWS flag which will give the number of rows found for the update, regardless of if they have the same value of what you're updating with.
Hope this helps.
I've taken this from my code so things like $link need to be in place- but it shows what you are interested in
function update() {
$q = "UPDATE table SET field1=? WHERE field2 = $value";
/* create a prepared statement */
$stmt = mysqli_stmt_init($link);
if (mysqli_stmt_prepare($stmt, $q)) {
mysqli_stmt_bind_param($stmt, "s", $field1);
mysqli_stmt_execute($stmt);
if(mysqli_stmt_errno($stmt)){
echo("Sql Error: ".$q. ' Sql error #: '.mysqli_stmt_errno($stmt). ' - ' . mysqli_stmt_error($stmt);
return false;
}
else{
$numrows = mysqli_stmt_affected_rows($stmt);
if (mysqli_stmt_errno($stmt) == 0 || mysqli_stmt_errno($stmt) ==''){
// numrows = -1 is flag no error and no rows affected
$numrows = ($numrows ==0?-1:$numrows);
}
else{
echo("Sql Error: ".$q. ' Sql error #: '.mysqli_stmt_errno($stmt). ' - ' . mysqli_stmt_error($stmt);
return false;
}
/* close statement */
mysqli_stmt_close($stmt);
return $numrows;
}
}
}
As per documentation on ROW_COUNT():
ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT. For other statements, the value may not be meaningful.
Your query:
Other than doing a SELECT before the UPDATE, is there SQL query or PHP functions that will tell me if the UPDATE matched any row
You can also use ROW_COUNT() within an UPDATE or any other DDL or DML statement.
Example: Using your pseudocode:
// pseudocode
UPDATE invoice SET due_date=? WHERE invoice_id=? AND creater_id=?;
IF ( ROW_COUNT() >= 1 ) THEN
UPDATE invoice_item SET price=? WHERE invoice_id=?
END IF;
Or else, you can try like:
UPDATE invoice SET due_date=? WHERE invoice_id=? AND creater_id=?;
UPDATE invoice_item SET price=
(case when ( row_count() >= 1 ) then ? else price end)
WHERE invoice_id=?;
Before setting the parameter value check again for the row_count() value to decide whether to set values for 1 or more parameters.
You can take this back to 1 query and not worry about affected rows:
UPDATE
invoice
left join invoice_item on invoice_item.invoice_id = invoice.invoice_id
SET
invoice.due_date = ?, -- the WHERE will only let this happen if it will be changed
invoice_item.price = ? -- the WHERE will only let this happen if it will be changed
WHERE
invoice.invoice_id = ?
and invoice.creater_id = ?
and invoice.due_date != ? -- here compare the new due_date to the one already in the db

Get Updated Value in MySQL instead of affected rows

I've been trying to find an answer to this question, but haven't found any definitive "yes" or "no" in all my research.
I'm running a simple MySQL query like this:
UPDATE item SET `score`=`score`+1 WHERE `id`=1
Is there a way for that query to return the updated value, instead of the number of rows affected? Just as a reference, I'm doing this in PHP, so the actual code looks like:
$sql = "UPDATE item SET `score`=`score`+1 WHERE `id`=1";
$new_value = mysql_query($sql);
//Unfortunately this does not return the new value
I know I could do a second query and just SELECT the value, but I'm trying to cut down on queries as much as possible. Is there a way?
You can do it with a stored procedure that updates, and then selects the new value into an output parameter.
The following returns one column new_score with the new value.
DELIMITER $$ -- Change DELIMITER in order to use ; withn the procedure
CREATE PROCEDURE increment_score
(
IN id_in INT
)
BEGIN
UPDATE item SET score = score + 1 WHERE id = id_in;
SELECT score AS new_score FROM item WHERE id = id_in;
END
$$ -- Finish CREATE PROCEDURE statement
DELIMITER ; -- Reset DELIMITER to standard ;
In PHP:
$result = mysql_query("CALL increment_score($id)");
$row = mysql_fetch_array($result);
echo $row['new_score'];
No, there's nothing like postgresql's UPDATE ... RETURNING output_expression in MySQL (yet?).
If you don't want to run another Query SELECT then here is another way to do it. I have modified Mr. Berkowski code for reference:
DELIMITER $$
CREATE PROCEDURE increment_score
(
IN id_in INT
)
BEGIN
set #newScore := null;
UPDATE item SET score = IF((#newScore := score+1) <> NULL IS NULL, #newScore, NULL) WHERE id = id_in;
SELECT #newScore;
END
DELIMITER ;
No you cant. You could make a function or stored procedure that could do the insert and return the updated value but that would still require you to execute two queries from within the function or stored procedure.
You can create a trigger, and you will know everything about the modifications.

MySQL: Updating all rows setting a field to 0, but setting one row's field to 1

Is there an efficient way to update a selection of rows' field to 0, but set One of these rows to 1 based on an ID.
Basically, I have multiple objects in a database, and I want to toggle between which one is "inuse", so the query will set one of the rows (by id) to inuse=1 and the others to inuse=0.
Thanks :)
UPDATE `table`
SET `inuse` = (`id` = 23)
Sure
UPDATE table SET inuse=IF(id=ABCD, 1, 0)
would set the inuse field to 1 if id is ABCD and 0 otherwise.
UPDATE table
SET inuse = (id = #id)
WHERE id = #id
OR inuse
This will update only relevant rows.
UPDATE myTable
SET Field = 0
WHERE FieldID <> [WhateverID]
UPDATE myTable
SET Field = 1
WHERE FieldId = [WhateverID]
Try
update tbl set inuse = if(test, 1, 0);
or shorter
update tbl set inuse = test;
for example
update tbl set inuse = name = 'foo';
If you are after to set a flag, so no other part of the code uses the same object at the same time, it's better if the calling code sets inuse=1 and resets it when done. Otherwise you will end up one thread to mark an object (row) as inuse, and then if another thread needs another object, it will reset the first one, while still in use.
If this is not the case, and you just want be able to set inuse for one, and reset all others, you can use:
UPDATE myTable
SET InUse = CASE
WHEN myTable.id = #id THEN 1
ELSE 0
END
if your database uses transactions, this is the best way to do it:
update myTable set inuse = 0;
update myTable set inuse = 1 where id = ?;
if you are not using transactions, then the other answer using CASE is the best option since it's portable. but it will require more CPU time than the two UPDATE statements.

Categories