fbsd_user wrote:
> Using this code I get this error message.
>
> Warning: mysql_num_rows(): supplied argument is not a valid MySQL
> result resource in /usr/local/www/data/mls_verifyemail.php on line 49
>
> What code should I use to check if the update worked or not?

Your second line does just that.

> $query = "UPDATE members SET email_verified='X' WHERE
> logon_id='".$logonid."'";
>
> $result = mysql_query($query) or die('Query couldn\'t
> executed:'.mysql_error());

Right there. You told php to die and print the error from mysql if the update failed.

> if (mysql_num_rows($result) == 1)
>   {
>     // the user id and password match,
>     print("User id on db");
>    }
>    else
>    {
>     //$errorMessage = 'Sorry, wrong user id / password';
>     print("Sorry, wrong user id / password");
>    }

The rest of the code only makes sense for a select, but you did an update. mysql_num_rows() tells the number of rows of data returned by a select. No select, no returned rows, no mysql_num_rows() -- hence the error message.

Logan, David (SST - Adelaide) wrote:
> Perhaps reading the manual would be a good start at
> http://us3.php.net/manual/en/function.mysql-num-rows.php
>
> It clearly states
>
> Retrieves the number of rows from a result set. This command is only
> valid for SELECT statements. To retrieve the number of rows affected by
> a INSERT, UPDATE, or DELETE query, use mysql_affected_rows().
>
> You are using a function not valid for an UPDATE

fbsd_user wrote:
>
> All ready read that and its clear as mud.
> It tells you update is different but gives no example.
>
> I asked in my post what should I code for checking the result of a
> update.

Please reread David's post, as he gave you the answer in the quote from the manual. Use mysql_affected_rows() to get the number of rows affected by an UPDATE. There are a few caveats, however. See the manual for details <http://www.php.net/manual/en/function.mysql-affected-rows.php>.

fbsd_user wrote:
> Maybe I have the overall logic wrong.

I think perhaps you do.

> I don't do a select query first to see if that record is there.
> I just try to update it.

Then it will be difficult to know for certain, in that scenario, whether or not a row was matched.

> If the logonid key in the update request is on the table, the record
> does get updated. I can verify that using phpmyadmin.
>
> When the logonid key in the update request is not on the table, the
> results checks still take the record successful updated condition. A
> look at the table using phpmyadmin shows me that there is no record
> matching that logonid key.

That's right.  Your query is

  "UPDATE members SET email_verified='X' WHERE logon_id=$logonid";

You are asking mysql to set the email_verified column to 'X' for every row in the table which has the given logon_id. The "success" of this query is not determined by the number of rows matched. If no rows match, mysql will "successfully" update 0 rows (just as it will "successfully" update 13 rows, if 13 rows match). That is not an error, as it is precisely what you requested.

> My last test I tried this
> if ($results == TRUE)
>
> and still the update was successful condition is taken even when the
> update key value is not on the table. I would expect the update was
> unsuccessful condition to have been taken.

No.  The query worked.  It successfully updated all 0 matching rows.

> So the basic question boils down to why does the successful
> condition always get taken even when there is no match on the table
> for the key value being used?

Because success of a query does not depend on the existence of rows which match its WHERE clause. Success depends on parsing and executing the query.

> This is testing a new developed script, so there may be a logic
> error in how things are done in the script. But I need to have the
> results of the update to be able to tell difference between a good
> update and one where the key used to target the record is not on the
> table.
>
> How do you suggest I should code the result condition test?

As David suggested, you can use mysql_affected_rows() to find how many rows were affected by your update. This will certainly be 0 if there is no matching row. You need to be aware, however, that it will also be zero if the matching row(s) already has email_verified='X', because mysql will not waste time changing a row for which the new value is the same as the old value. If that's a possibility, you could try parsing the outut of mysql_info(). See the manual for details <http://www.php.net/manual/en/function.mysql-info.php>.

Michael

--
MySQL General Mailing List
For list archives: http://lists.mysql.com/mysql
To unsubscribe:    http://lists.mysql.com/[EMAIL PROTECTED]

Reply via email to