Solved

MYSQL Update with result

Posted on 2014-02-21
8
562 Views
Last Modified: 2014-02-25
I have a simple mysql update query with an IF statement in it:

function questionResponseToStack()
{
	$id = $_POST['id'];
	
	$sql = "UPDATE questionresponses SET questionResponseDateUpdated = CURDATE(), questionResponseTimeUpdated = CURTIME(), questionResponseToStack = IF(questionResponseToStack=1, 0, 1) WHERE questionResponseID = '$id'";
	$result = mysql_query($sql);

	//handle your query.
	if ($result) {
		 $ajaxResponse['status'] = "success";
	} else {
		 $ajaxResponse['status'] = "failure";
		 $ajaxResponse['message'] = "There was a problem: " . mysql_error();
	}
	echo json_encode($ajaxResponse);
}

Open in new window


I want to be able to report back in the json if the record was changed to a 1 or 0. Can this be done easily and efficiently?
0
Comment
Question by:sjtinsley83
  • 3
  • 3
  • 2
8 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39877386
I'm not sure without making some changes to the SQL.  The reason I say that is that UPDATE queries may find and match rows, but unless there is an actual change in the contents of the row, the UPDATE did not occur and the number of affected rows will not increase.  So you may want to think through what is a success vs a failure.  MySQL will return TRUE whether any rows were updated or not.

In a semi-related matter, you've got a data base conversion ahead of you.  This article explains why PHP is eliminating the MySQL extension and what you must do to keep your scripts running.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
 

Author Comment

by:sjtinsley83
ID: 39877440
Oh! Any chance you have any pointers to help with the query?

Yeah I know the MySQL / PHP change but haven't got time to make change yet. The software runs locally offline on my webserver so I have no risk in it changing overnight.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 39877584
greetings  sjtinsley83, , As far as I have seen, there are few (if any) effective ways to "Combine" an UPDATE query and a SELECT query into one Query that updates AND returns a table column value,
So, What I would do for a simple update like this, is to do a SELECT first maybe as -
"SELECT questionResponseToStack FROM questionresponses  WHERE questionResponseID = '$id'";
and after sql query get the value as
$questionRes = $row['questionResponseToStack'];
$oppositeRes = 1;
if ($questionRes)  $oppositeRes = 0;

do your UPDATE query and then in the return message have -

if ($result) {
    $ajaxResponse['status'] = "success";
    $ajaxResponse['message'] = 'questionResponseToStack was '.$questionRes.' and is now '.$oppositeRes;
    }
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39877971
Help me understand the intent of the query a little bit, please.  Can you just say in English what the query is supposed to do?  Also, what is the reason for separating the date and time columns?  Thanks.
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

Author Comment

by:sjtinsley83
ID: 39878112
Basically its is toggling a button on or off... And I wanted to pass back the state of the button each time rather than relying on javascript to keep the color of the button in sync.

The reason for separating the date time..... erm.... because  I wanted to?? :)
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39878200
OK, that makes more sense to me now.  I'm assuming that the id field is UNIQUE.   Here are the conditions that can exist:

1. There is no record to match the request
2. There is a record with a zero
3. There is a record with a one

If you have an index on each of id and questionResponseToStack, you can run an amazingly fast SELECT query that will tell you which of the three conditions exists.  You can save that information for your JSON response.  Then you can run either an INSERT or an UPDATE query, depending on whether you're looking at 1, 2, or 3.  The speed of these queries is likely to be such that no amount of tinkering with them will yield a meaningful improvement in script performance.
http://xkcd.com/1205/

It may not particularly matter, but for me, it's easier to think about sorting with the DATETIME information in just one column.
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 500 total points
ID: 39878526
OK, I saw about your reason as "toggling a button on or off." so you can add an element to the ajaxResponse array for the button, OR you can do the select query After the update and use the returned value.

if ($result) {
    $ajaxResponse['status'] = "success";
    $ajaxResponse['button'] = $oppositeRes;
    }

BUt maybe you do not consider my suggestion will work? I am not sure from reading your question again, that the code you have posted already works for you? ? , , , OR you have not tried it and wanted some other code?
0
 

Author Closing Comment

by:sjtinsley83
ID: 39886749
Looks like the best way to do this is to have an insert query, and a select query.
thanks for your help.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

8 Experts available now in Live!

Get 1:1 Help Now