Solved

Best practice for checking value of a field in php/mysql?

Posted on 2011-09-06
2
251 Views
Last Modified: 2012-06-27
Hi,
in my app I need to check the value of a field and based on that, insert new value or not.
What I'm currently doing is:

 
$result = mysql_query("SELECT subscribestatus FROM callers WHERE FromCaller = '$From'");
	while ($row = mysql_fetch_assoc($result)) {
	$status = $row["subscribestatus"];
	}

Open in new window


Then once its assigned as a variable, I use a switch/case statement to check the value compared to the 3 different ones it may be. Then if it matches one of them, I do an update statement something like this:

 
mysql_query("UPDATE callers SET subscribestatus = 'Subscribed' WHERE FromCaller = '$From' AND subscribestatus = 'Pending'");?>

Open in new window


So my question is: is that the best way to do it? Seems like a lot of work to do a comparison of one value. I just get a nagging suspicion there is a better way.

Thanks tj
0
Comment
Question by:tjyoung
2 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 36490050
Regarding this: $status = $row["subscribestatus"];

You do not need to do that.  $row["subscribestatus"] is already its own variable.  There is no value added by proliferating variables.  You can use switch / case on the original element of the $row array

Your design pattern makes sense to me as you have described it here.  The things I would be concerned about include the origin and sanitization of the $From variable, and the absence of the LIMIT clause in your queries.

HTH, ~Ray
0
 
LVL 26

Expert Comment

by:skullnobrains
ID: 36490057
you can perform a single SQL query using an if or case statement to update conditionnally to the existing or another value. something like this :

"UPDATE callers SET subscribestatus =
  CASE WHEN (SELECT subscribestatus FROM ...) = 'something_meaning you do not want to update'
  THEN subscribestatus
  ELSE 'Subscribed'
  END WHERE FromCaller = '$From' AND subscribestatus = 'Pending'"

this is already a little better but it will still perform the lookup and lock the row in the callers table

note this will also work if you stick the extra comparison in the where clause

... WHERE ... AND  (select ...) = 'whatever_triggers_the_update'

otherwise you need to go for a transaction : perform the select
SELECT @var:=subscribestatus FROM ...;

CASE when @var == whatever
then UPDATE ...

i'm not completelyy sure about the syntax for the transaction part. feel free to ask if you need more help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now