473,414 Members | 1,751 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,414 software developers and data experts.

PHP/MySQL update: counting unmatched records


Folks,

When performing an update in mysql (using PHP), can I find out how many
records were matched?

mysql_affected_rows() won't work... and I have the following problem
that I thought I could resolve with a simple function:

Example:
I have 50records - I want to update a selection of the recods - some,
but not all the records that I want to update will exist - Each record
has a unique key - I have the unique keys for each record I want to
update - Some of the records might already have the data that I want to
write (thus update can be skipped/ignored/remain unchanged).

When I perform the changes from the MySQL command line, I get the
following results

- Update a record where the key is found and changes made:
Rows matched: 1 Changed: 1 Warnings: 0

- Update a record where they key is found, but record and update are the
same (so no need to update since no changes required)
Rows matched: 1 Changed: 0 Warnings: 0

- Update requested but record not found (thus no change made)
Rows matched: 0 Changed: 0 Warnings: 0

When I perform the update with mysql_query, I get a return of TRUE for
all three conditions above - I would at very least expect the last case
would return FALSE (where the record wasn't found thus not updated).

Can anybody tell me how, using a PHP MySQL function I could find out the
number of records changed, or not changed? If not, I guess I first have
to perform a select to confirm all records exist before doing the
update, or can someone suggest an alternative solution?

All help, via the newsgroup, is much appreciated, thanks
randell d.

Jul 17 '05 #1
2 3818
Reply via newsgroup:
When performing an update in mysql (using PHP), can I find out how many
records were matched?

mysql_affected_rows() won't work... and I have the following problem
that I thought I could resolve with a simple function:

Example:
I have 50records - I want to update a selection of the recods - some,
but not all the records that I want to update will exist - Each record
has a unique key - I have the unique keys for each record I want to
update - Some of the records might already have the data that I want to
write (thus update can be skipped/ignored/remain unchanged).

When I perform the changes from the MySQL command line, I get the
following results

- Update a record where the key is found and changes made:
Rows matched: 1 Changed: 1 Warnings: 0

- Update a record where they key is found, but record and update are the
same (so no need to update since no changes required)
Rows matched: 1 Changed: 0 Warnings: 0

- Update requested but record not found (thus no change made)
Rows matched: 0 Changed: 0 Warnings: 0

When I perform the update with mysql_query, I get a return of TRUE for
all three conditions above - I would at very least expect the last case
would return FALSE (where the record wasn't found thus not updated).
You only get a false return value from mysql_query if there was an error
executing the query. Unfortunately there is no way to retrieve the number
of records that matched the query (without going through the CLI).
Can anybody tell me how, using a PHP MySQL function I could find out the
number of records changed, or not changed? If not, I guess I first have
to perform a select to confirm all records exist before doing the
update, or can someone suggest an alternative solution?


What you will have to do is use a count query to figure out how many rows
will match. Knowing this you can execute the query and use
mysql_affected_rows to see how many rows were actually changed.

André Næss

Jul 17 '05 #2
André Næss wrote:
Reply via newsgroup:

When performing an update in mysql (using PHP), can I find out how many
records were matched?

mysql_affected_rows() won't work... and I have the following problem
that I thought I could resolve with a simple function:

Example:
I have 50records - I want to update a selection of the recods - some,
but not all the records that I want to update will exist - Each record
has a unique key - I have the unique keys for each record I want to
update - Some of the records might already have the data that I want to
write (thus update can be skipped/ignored/remain unchanged).

When I perform the changes from the MySQL command line, I get the
following results

- Update a record where the key is found and changes made:
Rows matched: 1 Changed: 1 Warnings: 0

- Update a record where they key is found, but record and update are the
same (so no need to update since no changes required)
Rows matched: 1 Changed: 0 Warnings: 0

- Update requested but record not found (thus no change made)
Rows matched: 0 Changed: 0 Warnings: 0

When I perform the update with mysql_query, I get a return of TRUE for
all three conditions above - I would at very least expect the last case
would return FALSE (where the record wasn't found thus not updated).

You only get a false return value from mysql_query if there was an error
executing the query. Unfortunately there is no way to retrieve the number
of records that matched the query (without going through the CLI).

Can anybody tell me how, using a PHP MySQL function I could find out the
number of records changed, or not changed? If not, I guess I first have
to perform a select to confirm all records exist before doing the
update, or can someone suggest an alternative solution?

What you will have to do is use a count query to figure out how many rows
will match. Knowing this you can execute the query and use
mysql_affected_rows to see how many rows were actually changed.

André Næss


Thanks for that!

randelld
Jul 17 '05 #3

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

Similar topics

7
by: Gary | last post by:
I haver a table of students - Say 100 students that I need to be able to update/delete and amend. I know I can do this one student at a time which is simple but lets say I want to see all the...
6
by: PG | last post by:
When deleting a row from the database, that id is now missing. So what I'm trying to do is update in a loop (maybe an sql loop if there is one) of all of the id numbers - sort of like renaming...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
2
by: Colleyville Alan | last post by:
I want to extract some records that are common to three tables, but not contained in the fourth. Following what I have see in the archives and also trying the unmatched records query wizard, if I...
2
by: David | last post by:
Hi, Has anyone had this problem ? I am using MySQL ODBC 3.51 Driver, with MS Access 2003 and MySQL 4.1.11 standard log. I created my tables in MS Access, then exported them via ODBC to an...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
1
by: csgraham74 | last post by:
Hi Guys, I was wondering if someone could help me with an access query. I basicallly have two tables A & B Im table A there are numerous records with Fields1 & fields 2 I want to use...
110
by: alf | last post by:
Hi, is it possible that due to OS crash or mysql itself crash or some e.g. SCSI failure to lose all the data stored in the table (let's say million of 1KB rows). In other words what is the worst...
16
by: ARC | last post by:
Hello all, So I'm knee deep in this import utility program, and am coming up with all sorts of "gotcha's!". 1st off. On a "Find Duplicates Query", does anyone have a good solution for...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.