Solved

MYSQL Update with result

Posted on 2014-02-21
8
565 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:Steve Tinsley
  • 3
  • 3
  • 2
8 Comments
 
LVL 109

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:Steve Tinsley
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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
LVL 109

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
 

Author Comment

by:Steve Tinsley
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 109

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:Steve Tinsley
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
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…

803 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