473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

select on updated value during the fetch

Hi i have a table with all value at 4
i select all lines in a fetch
i update one with a value of 7

i update all the row in the fetch with a value 5

the result is that all my row are at 5
and the only line which should be at 7 is at 5 too.

i see that the problem is at the first mysql_db_query, and i don t know
how to solve it

thanks
$query="SELECT * FROM val WHERE str='4'";
$result=mysql_db_query($datamysql,$query);
$query7="UPDATE val SET str='7',updated='1' WHERE val='2'";
$result7=mysql_db_query($datamysql,$query7);

while ($row=mysql_fetch_array($result))
{

$str=$row["str"];
$val=$row["val"];
$query2="UPDATE val SET str='5' WHERE val='$val'";
$result2=mysql_db_query($datamysql,$query2);
}

Jun 16 '06 #1
5 1826
<cd****@relayeur.com> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
Hi i have a table with all value at 4
i select all lines in a fetch
i update one with a value of 7
This is where it goes wrong. You update nothing, see explanation below.
i update all the row in the fetch with a value 5

the result is that all my row are at 5
and the only line which should be at 7 is at 5 too.

i see that the problem is at the first mysql_db_query, and i don t know
how to solve it

thanks
$query="SELECT * FROM val WHERE str='4'";
$result=mysql_db_query($datamysql,$query);
$query7="UPDATE val SET str='7',updated='1' WHERE val='2'";
You update all rows whose val=2 (should this be 4 maybe?), that is none,
because all rows val=4. Nothing is updated. Even if it was, the old record
that you fetched earlier are intact, only the database is updated but the
updates do not reflect on already fetched records. So when you select
something, then update something, the old values are still inside $result,
so the updating means nothing again.
$result7=mysql_db_query($datamysql,$query7);

while ($row=mysql_fetch_array($result))
{

$str=$row["str"];
$val=$row["val"];
$query2="UPDATE val SET str='5' WHERE val='$val'";
$result2=mysql_db_query($datamysql,$query2);
}

Jun 16 '06 #2
the update works for the updated column
but as you say the prefetch row are already there
so the mysql_fetch_array command will take a line where there is not
anymore the str=4 value

i know why i have this problem and i like to know how not have it
:-)

thx

Kimmo Laine a écrit :
<cd****@relayeur.com> wrote in message
news:11*********************@c74g2000cwc.googlegro ups.com...
Hi i have a table with all value at 4
i select all lines in a fetch
i update one with a value of 7


This is where it goes wrong. You update nothing, see explanation below.
i update all the row in the fetch with a value 5

the result is that all my row are at 5
and the only line which should be at 7 is at 5 too.

i see that the problem is at the first mysql_db_query, and i don t know
how to solve it

thanks
$query="SELECT * FROM val WHERE str='4'";
$result=mysql_db_query($datamysql,$query);
$query7="UPDATE val SET str='7',updated='1' WHERE val='2'";


You update all rows whose val=2 (should this be 4 maybe?), that is none,
because all rows val=4. Nothing is updated. Even if it was, the old record
that you fetched earlier are intact, only the database is updated but the
updates do not reflect on already fetched records. So when you select
something, then update something, the old values are still inside $result,
so the updating means nothing again.
$result7=mysql_db_query($datamysql,$query7);

while ($row=mysql_fetch_array($result))
{

$str=$row["str"];
$val=$row["val"];
$query2="UPDATE val SET str='5' WHERE val='$val'";
$result2=mysql_db_query($datamysql,$query2);
}


Jun 16 '06 #3
<cd****@relayeur.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
the update works for the updated column
but as you say the prefetch row are already there
so the mysql_fetch_array command will take a line where there is not
anymore the str=4 value

i know why i have this problem and i like to know how not have it
:-)

Run the queries in different order. First update, then select.

like this:

$query7="UPDATE val SET str='7',updated='1' WHERE val='2'";
$result7=mysql_db_query($datamysql,$query7);

$query="SELECT * FROM val WHERE str='4'";
$result=mysql_db_query($datamysql,$query);

while ($row=mysql_fetch_array($result))
{
$str=$row["str"];
$val=$row["val"];
$query2="UPDATE val SET str='5' WHERE val='$val'";
$result2=mysql_db_query($datamysql,$query2);
}

Well actually, to keep it very very very simple, you could just do this:

$query7="UPDATE val SET str='7',updated='1' WHERE val='2'";
$result7=mysql_db_query($datamysql,$query7);
$query2="UPDATE val SET str='5' WHERE str='4'";
$result2=mysql_db_query($datamysql,$query2);

And nothing else. If you want to update all rows where str='4', then use
that as the condition, you don't need to first select something with one
condition, and then one by one row update all the rows. You can do it all in
one update query.

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Jun 16 '06 #4
in fact, i need this order because i am rebuilding a table and i need
to do this way

i have a workaround with generating no fetch of all the table, but to
select the rows one by one
which give a stupid number of query( 200 000 rows=>200 000 query) and
it take a lot of time naturally :-)

i made this example to show my problem

is not there a possibility to fetch a special way which can take in
consideration freshness?
Kimmo Laine a écrit :
<cd****@relayeur.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
the update works for the updated column
but as you say the prefetch row are already there
so the mysql_fetch_array command will take a line where there is not
anymore the str=4 value

i know why i have this problem and i like to know how not have it
:-)

Run the queries in different order. First update, then select.

like this:

$query7="UPDATE val SET str='7',updated='1' WHERE val='2'";
$result7=mysql_db_query($datamysql,$query7);

$query="SELECT * FROM val WHERE str='4'";
$result=mysql_db_query($datamysql,$query);

while ($row=mysql_fetch_array($result))
{
$str=$row["str"];
$val=$row["val"];
$query2="UPDATE val SET str='5' WHERE val='$val'";
$result2=mysql_db_query($datamysql,$query2);
}

Well actually, to keep it very very very simple, you could just do this:

$query7="UPDATE val SET str='7',updated='1' WHERE val='2'";
$result7=mysql_db_query($datamysql,$query7);
$query2="UPDATE val SET str='5' WHERE str='4'";
$result2=mysql_db_query($datamysql,$query2);

And nothing else. If you want to update all rows where str='4', then use
that as the condition, you don't need to first select something with one
condition, and then one by one row update all the rows. You can do it allin
one update query.

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)


Jun 16 '06 #5
cd****@relayeur.com wrote:
in fact, i need this order because i am rebuilding a table and i need
to do this way

i have a workaround with generating no fetch of all the table, but to
select the rows one by one
which give a stupid number of query( 200 000 rows=>200 000 query) and
it take a lot of time naturally :-)

i made this example to show my problem

is not there a possibility to fetch a special way which can take in
consideration freshness?


No. Once rows are fetched, MySQL (or any other RDB) will not update an already
fetched value just because it has been updated. That's a big reason why you
shouldn't update tables while fetching data, unless you're updating the data you
just fetched.

P.S. Please don't top post.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jun 16 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Rodusa | last post by:
I am trying to assign @sql variable to @total, where @sql is a sql statement stored on the database, however what I am getting is its string value and not its calcuation. Could anybody help? ...
12
by: TP | last post by:
Here is my problem. I need to display a table about which I have no information except the table name. Using metadata I can somehow show the column names and record values. But my table has 1...
13
by: RR | last post by:
Hi, It appears that DB2 has an arbitrary restriction on the use of "fetch first N rows". I want to insert into a table from a large table query, but only insert the first N rows: insert...
8
by: Jacob Arthur | last post by:
How would I go about using a custom select string that is passed from a form to the SelectCommand parameter of SqlDataSource? I tried: SelectCommand = "<% Request.Form("hdnSelect") %>" but I...
3
by: taru | last post by:
Env: DB2 UDB 8.1 on AIX We are executing a db2 sql stored procedure which is invoked from a java client. The stored procedure contains few select, insert and update statements. Using the DB2...
1
by: bughunter | last post by:
simple query select * from "Result" res where (res."QID" = 51541 or res."QID" = 51542) works fine ("SRV-BL"."Result" ~ 900000 rows) and returns 36 rows but update - no! update...
2
by: mokazawa1 | last post by:
Hi, I'm using stored procedures in DB2 UDB 8.1.2. In this stored, I execute a select for update command, opening a cursor. Then I update the rows using fetch and current of. The problem is that...
5
by: Mahesh S | last post by:
Hi I would like to write a SQL select statement that would fetch rows numbered 50 to 100. Let me clarify, if i say "fetch first 10 rows only", it returns the first 10 rows from the resultset....
3
by: jbutler8192 | last post by:
Hello. We have a multi-user Java application that can connect to either a UniVerse database or a DB2 database on an AS400 through JDBC. The program executes a SELECT ... FOR UPDATE statement....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.