Unix Technical Forum

Re: mysql_error on an update

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 ...


Go Back   Unix Technical Forum > Database Server Software > MySQL

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-28-2008, 07:19 AM
Gary L. Burnore
 
Posts: n/a
Default Re: mysql_error on an update

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
================================================== =========================
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-28-2008, 07:19 AM
Bill Karwin
 
Posts: n/a
Default Re: mysql_error on an update

> 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.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 06:03 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com