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. 2 3847
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
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_affecte d_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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 students on the screen at the same
time, modify some, mark some for deletion and even have blank fields at the
end to add a new record.
In HTML which is generated I label each row and input field with a
name/number combination i.e <input type=text...
|
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 them.
It did partly work because all the id's were set to 22. Thats because
there was 22 rows.
Here's the code I used:
$result = mysql_query ("SELECT * FROM counter");
for($counting = 1; $row = mysql_fetch_row ($result); ++$counting)
|
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 version of this document can be found at:
http://prdownloads.sourceforge.net/souptonuts/README_mysql.txt?download
|
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 only use one of those three
tables and use a LEFT JOIN to the fourth and set the target field in the
fourth table to null, I get the unmatched records.
However, if I link the three tables together and then want to exclude
records in the fourth...
|
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
externally hosted MySQL database (fasthosts) . I then import-linked
| |
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
communicate with a MySQL database table on a web server, from inside
of my company's Access-VBA application. I know VBA pretty well but
have never before needed to do this HTTP/XML/MySQL type functions.
|
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 table B to update table A. Bascically i need to compare
table A & Table B - if records exist in Table B that are not in Table A
|
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 case scenario for MyISAM
backend?
Also is it possible to not to lose data but get them corrupted?
|
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
renaming the duplicate records? My thinking was to take the results of the
duplicate query, and somehow have it number each line where there is a
duplicate (tried a groups query, but "count" won't work), then do an update
query to change the duplicate to...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |