Suppose I am firing query that will upadte data if all value are available else will run with 0 rows updated. So How to get those 0 rows updated query/data from Oracle db in PHP?
In my script, I am updating table if item_flag is N, item_name is stored in $sku so checking with that & site_code is in $final_code so all these in where condition.
so if update query runs successfully with value updation then I am taking execution result in $result & updating status of Mysql table.
But what happened is when there is some data not present say $sku in update query then query runs with 0 rows updated...& likewise it will move to if($result) loop & update the status which I don't want as practically data/row is not get updated...
so How to get 0 rows updated query/data from Oracle in PHP?
--------------some code above--------
$query_ora_update = "UPDATE ITEM_DETAILS SET ITEM_FLAG= 'Y', LAST_UPDATE_DATE = sysdate WHERE ITEM_NAME = '$sku' AND SITE_CODE = '$final_code' AND ITEM_FLAG = 'N' ";
$parse_result = oci_parse($conn,$query_ora_update);
$result = oci_execute($parse_result);
oci_commit($conn);
if($result)
{
$query_update_alert = "UPDATE product_creation SET alert_status =1 where
entity_id = $entity_id and sku = '$sku' and alert_status = 0";
$result_query_update_alert = mysql_query($query_update_alert);
}
-------------------------------
Use oci_num_rows function. Documentation Here
EDIT: Example Code
$parse_result = oci_parse($conn,$query_ora_update);
$result = oci_execute($parse_result);
$row_count = oci_num_rows($parse_result);
oci_commit($conn);
if($row_count > 0)
{
-------------------
Related
Now i have this sql update code:
$sql = "UPDATE products SET stock = stock - '$quantity' WHERE product_id = '$id'";
I have some problem with this. It works, but if the stock value in the table is smaller, than the quantity in the webshop cart, it will update the stock field to a minus value.
I know, that there is a function in mysql for this, that will only update it to 0, and not to minus. Whats that function, or how should i do this?
You can use case
UPDATE products SET stock
= case
when stock > '$quantity' then stock - '$quantity'
else 0 end
WHERE product_id = '$id';
Be sure that your variables $quantity and $id are checked to be sure that they are numeric to avoid SQL injection.
You can use the MySQL IF Statement for this. First check if stock - quantity is greater than 0 then return the value, else return 0.
UPDATE products
SET stock = IF(stock - '$quantity' > 0, stock - '$quantity', 0)
WHERE product_id = '$id';
You could use the GREATEST function to get the largest value of your existing calculation or 0, then if the current calculation is negative, 0 will be inserted.
I haven't tested this, but this should work:
$sql = "UPDATE products SET stock = GREATEST(stock - '$quantity', 0) WHERE product_id = '$id'";
I'd just be careful you aren't prone to SQL injection here as you are passing $quantity and $id directly to the query string and if this is from a $_POST or $_GET request variable then the query can be manipulated.
Trying to run a MySQL update in Excel.
Would like to check if the UPDATE happened or not, i.e. was the row found and updated or such a row does not exist and 0 rows were updated
Tried to find a return code from MySQL in the VBA code but nothing I am at a loss!
c = 2
Do While Trim(MAIN.Cells(c, 1)) <> ""
sMyword = "UPDATE table1 SET price = 1.00 WHERE part = 'ABC';"
conn.Execute sMyword
If "did the update happen against the row" > 0 Then
MAIN.Cells(c, 3) = "Price UPDATED OK"
Else
MAIN.Cells(c, 3) = "Part NOT FOUND "
MAIN.Cells(c, 3).Interior.ColorIndex = 3 'Red
End If
c = c + 1
Loop
I wish to get the number of rows updated by the SQL statement sMyword
The Execute method should return the affected records in a second argument RecordsAffected. Try this syntax:
Dim RecordsAffected As Long
sMyword = "UPDATE table1 SET price = 1.00 WHERE part = 'ABC';"
conn.Execute sMyword, RecordsAffected
MsgBox RecordsAffected
reference: https://learn.microsoft.com/en-us/sql/ado/reference/ado-api/execute-method-ado-connection?view=sql-server-2017
I believe this is causing anywhere from a 5 minute to 20 minute delay depending on the number of records. I need to translate it into a LEFT JOIN but need some help getting it there.
qry_arr = array(':bill_type' => "INT");
$sql = "update ".$billing_table." c set c.bill_type = :bill_type";
$sql .= " WHERE NOT EXISTS (SELECT s.abbreviation FROM state s WHERE s.abbreviation = c.out_location)";
$sql .= " and c.out_location != 'UNKNOWN' and c.out_location != ''";
UPDATE $billing_table c
LEFT JOIN state s ON s.abbreviation = c.out_location
SET c.bill_type = :bill_type
WHERE s.abbreviation IS NULL
AND c.out_location NOT IN ('UNKNOWN', '')
This is essentially the same as the syntax for a SELECT for the rows that don't match. See Return row only if value doesn't exist. Just replace SELECT ... FROM with UPDATE, and insert the SET clause before WHERE.
Make sure you have indexes on out_location and abbreviation.
I am not sure if its possible or not, Just want to know if it is. I have column plan_popular which has default value 0. Lets same i have a list :
Plan Name | plan_popular | amount
===================================
plan A 0 25.00
plan B 1 50.00
plan C 0 90.00
This is how i am doing:
$stmt = "update {CI}plans set plan_popular = 0";
$this->db->query($stmt);
$stmt2 = "update {CI}plans set plan_popular = 1 where plan_id = ?";
$this->db->query( $stmt2, array($plan_id) );
Now i have set the plan C to make. Now i want to reset it and want to make popular plan C to 1. What i am doing is running two queries, One i reset and make the plan_popular 0 and the second is get the update the plan C to 1 with there id. Is it possible in single query?
You can use an expression to determine the value to assign:
UPDATE {CI}plans
SET plan_popular = IF(plan_id = ?, 1, 0);
try this,
UPDATE {CI}plans
SET `plan_popular` = CASE `Plan Name`
WHEN 'plan C' THEN 1
ELSE 0
END
WHERE `Plan Name` IN((select `Plan Name` from {CI}plans where plan_popular=1 ) , 'plan C');
Updates can be expensive, what with manipulating locks, triggers and constraints firing, etc. In general, you want to avoid updating a field to the same value it already has. In English, if plan_id = variable and plan_popular is 0 then set it to 1 but if plan_id is any other value and plan_popular is 1 then set it to 0.
UPDATE {CI}Plans
SET plan_popular = if( plan_id = ?, 1, 0 )
where (plan_id = ? and plan_popular = 0)
or (plan_id <> ? and plan_popular = 1);
The where clause lets through only those rows that will actually be changed by the update. If this is a largish table, that can make quite a difference in response time. Logic is much less expensive than any actual operation that can performed in the database.
I have a database similar to this:
table name = jos_school ... id = Primary key, name = Unique key
id name no_of_students no_of_staffs fees
1 schoolA 0 0 0
2 schoolB 0 0 0
...
...
In phpMyAdmin I did something like this, and it worked (successfully updated multiple rows and columns),
UPDATE jos_school
SET no_of_students = CASE name
WHEN 'schoolA' THEN '1523'
WHEN 'schoolB' THEN '546'
....
END,
no_of_staffs = CASE name
WHEN 'schoolA' THEN '1234'
WHEN 'schoolB' THEN '346'
....
END
WHERE name IN ('schoolA', 'schoolB')
However, I was not able to update the table USING JOOMLA's Update methods. I don't want to use foreach and update the table over 1000 times. I want to execute a single query.
I've also read: http://docs.joomla.org/Inserting,_Updating_and_Removing_data_using_JDatabase#Updating_a_Record
and dint found it helpful in this case.
So, can someone point me to the right direction.
You can query as below,
// Create a new query object.
$db = &JFactory::getDBO();
$query = $db->getQuery(true);
// Select the required fields from the table.
$query="UPDATE jos_school SET no_of_students = CASE name";
foreach($data as $d)
{
$query.=".....";
}
$query.=".....";
$db->setQuery((string)$query);
Hope that helps ...