Re : [PHP-DB] MySQL Update Quandry

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




Message-ID: <9e6f1c320511270949jca5a7dq710c4be8c017dc46@xxxxxxxxxxxxxx>
Date: Sun, 27 Nov 2005 12:49:17 -0500
From: Glenn Deschênes <maestroqc@xxxxxxxxx>
To: php-db@xxxxxxxxxxxxx
MIME-Version: 1.0
Content-Type: multipart/alternative;
        boundary="----=_Part_11492_6161784.1133113757293"
Subject: Re:  MySQL Update Quandry

This is not the problem.
The query fails when there is, as examples, an invalid syntax in the query
or the table is not found.

That's correct, and there's a distinct lack of understanding going around the list.

A query may or may not return rows, and that is not a sign of "failure". You might class it as failing, but returning no rows is a sign that there's no data matching your query - in which case you may have no matching data, or you may have written a valid query but one which doesn't ask the right question ;-)

So, checking mysql_error() return string will only tell you if the query resulted in a database error.
It will not tell you if there were no results or changes to the database.

However, if the query executes but updates nothing. How can I know when the
update has changed a row or not? Not changing a row does not return a
failure.

PHP has functions for this, which you should read in conjunction with the MySQL3.24 documentation to understand when the DB considers a row inserted, deleted or updated.

mysql_affected_rows : http://us2.php.net/mysql-affected-rows

MySQL 4 and 5 have additional SQL statements to get these values after the insert / update, using a second query: SQL_CALC_FOUND_ROWS / FOUND_ROWS(), LAST_INSERT_ID() LAST_INSERT_ID, ROW_COUNT()

For example, updating a row and giving it the exact same values will result in an update row count of zero.

Inserting should always (unless the query fails) give an affected_rows value more than zero.

Inserting *may* also result in a insert_id value : *If* the table has a primary key, *and* it's an autoincrement primary key, it'll return the autoincrement value) : http://us2.php.net/mysql_insert_id


Matt's solutions works well. Since the application is hosted and I have
limited choices... the mysql_info() does the trick.

Although it's not the best use for flow control for this task, because you have to parse the string to get values from it (it's only available PHP 4.3.0 onwards, which shouldn't be a problem on this list), and it's not giving enough information unlike mysql_error()

HTH
Cheers - Neil
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux