This is a discussion on Re: mysql_error on an update within the MySQL forums, part of the Database Server Software category; --> On 31 Jan 2006 07:44:15 -0800, "Jeff" <joesiege@gmail.com> wrote: >What's the best practice for handling the following situation, when ...
| |||||||
| Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| ||||
| On 31 Jan 2006 07:44:15 -0800, "Jeff" <joesiege@gmail.com> wrote: >What's the best practice for handling the following situation, when I >do an update like this: > >$sql = "UPDATE haha SET papa="loco" WHERE id=$var"; >$res = mysql_query($sql,$db); > >If I don't get a match in my where clause, i.e., 12!=44 the UPDATE does >not occur but mysql_errno == 0 and mysql_error = "" so I can't capture >the failure. > >Any thoughts? Two. Do a select to count how many rows might need to be updated before doing the update and remember that there's a comp.databases.mysql group. -- gburnore at DataBasix dot Com --------------------------------------------------------------------------- How you look depends on where you go. --------------------------------------------------------------------------- Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ Official .sig, Accept no substitutes. | ÝÛ³ºÝ³Þ³ºÝ³³Ýۺݳ޳ºÝ³Ý³Þ³ºÝ³ÝÝÛ³ | ÝÛ 0 1 7 2 3 / Ý³Þ 3 7 4 9 3 0 Û³ Black Helicopter Repair Services, Ltd.| Official Proof of Purchase ================================================== ========================= |
| ||||
| > On 31 Jan 2006 07:44:15 -0800, "Jeff" <joesiege@gmail.com> wrote: > >>What's the best practice for handling the following situation, when I >>do an update like this: >> >>$sql = "UPDATE haha SET papa="loco" WHERE id=$var"; >>$res = mysql_query($sql,$db); >> >>If I don't get a match in my where clause, i.e., 12!=44 the UPDATE does >>not occur but mysql_errno == 0 and mysql_error = "" so I can't capture >>the failure. >> >>Any thoughts? Hi Jeff, It's actually not an error for an update or delete statement to affect zero rows, believe it or not. Just as it's not an error for a select query to return zero rows. But there may be a solution for you. Check out the mysql_affected_rows function in PHP. http://us3.php.net/manual/en/functio...ected-rows.php By the way, I'd like to give you a gentle reminder to do some verification on the $var variable in your code to make sure it contains a valid integer and no other string. If the value is coming from a request parameter, a malicious user could enter a string such as "44 OR 1=1" and mess up your database! This is called SQL injection, it's a common security vulnerability. See http://en.wikipedia.org/wiki/Sql_injection Regards, Bill K. |