Re: To check for existing user in database

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

 



2009/1/16 Shawn McKenzie <nospam@xxxxxxxxxxxxx>:
> Lars Torben Wilson wrote:
>> 2009/1/15 tedd <tedd.sperling@xxxxxxxxx>:
>>> At 9:46 AM -0800 1/15/09, Chris Carter wrote:
>>>
>>> Chris:
>>>
>>> That's not the way I would do it. After establishing a connection with the
>>> database, I would use the query:
>>>
>>> $query "SELECT email FROM owners WHERE email = '$emailAddress' ":
>>> $result = mysql_query($query) or die(mysql_error());
>>>
>>> if(mysql_affected_rows())
>>>   {
>>>   // then report a duplicate email/record.
>>>   }
>>> else
>>>   {
>>>  // else insert a new record in the dB.
>>>   }
>>>
>>> HTH's
>>>
>>> tedd
>>
>> You want to use mysql_num_rows() there instead of
>> mysql_affected_rows(). (Just a typo in this case, I suspect, but for
>> the benefit of the less experienced it's worth pointing out.)
>>
>> For the newer PHP users, mysql_num_rows() tells you the number of rows
>> you found with a SELECT query, while mysql_affected_rows() tells you
>> how many rows you affected with an INSERT, UPDATE, REPLACE INTO, or
>> DELETE query.
>>
>>
>> Regards,
>>
>> Torben
>
> mysql_num_rows() may make more sense, however mysql_affected_rows() will
> work the same with a select.  The PHP mysql_affected_rows() calls the
> MySQL mysql_affected_rows(), which states:
>
> "For SELECT statements, mysql_affected_rows() works like mysql_num_rows()."

(My apologies for not following the thread for a week. . .)

Yes, you are right, except that the restriction isn't with MySQL, it's
within PHP. The problem is that if you leave out the optional resource
argument it works like you describe, but if you include the argument,
PHP barfs. It's good practice to use the one intended for the purpose
at hand, even if the other will work in some (or even most)
situations.

I suppose this is a bug in PHP in that it should really behave the way
that the MySQL API does to avoid surprises, but it does illustrate the
point that using the intended function is easier in the long run: you
know it's been tested against its intended usage and not necessarily
against others.


Regards,

Torben

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux