Solved

MYSQL Update with result

Posted on 2014-02-21
8
558 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:sjtinsley83
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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 …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

772 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

11 Experts available now in Live!

Get 1:1 Help Now