Solved

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

Posted on 2011-09-06
2
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 110

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 27

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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

624 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