Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2011-09-06
2
Medium Priority
?
290 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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