Jump to content

Increase an id by 1 quickly?


galvin

Recommended Posts

If you have a simply query like the one below, and you want to set an id equal to itself PLUS 1, is there an easy way to do that?

 

For example, if the field "someid" in the mysql table was equal to 6, I'd want the code below to set it equal to 7.  The part in parentheses is what I need help with.  Or is it possible that is actually how i would do it?  The value of someid would change a lot, hence my question.

 

My first thought was that I'd have to grab the data first via a separate query and then put the someid value into a variable called $number and then use "($number + 1)" in the next query. But I feel like there has to be a quicker way to simply increase an id by 1.

 

 

$sql = "UPDATE answers SET someid=(someid +1)
				WHERE something=$something";

 

Link to comment
Share on other sites

The exact way you have it won't work because you'd have to end the quotes around the variable name, use + around the varname, then enclose the rest of the query in quotes.  Also, javascript isn't like php--you can't just hit return and continue a string on the following line.  To concatenate a string variable you have to a trailing space and a backslash:

alert("This is a sample string, and although I'm coding /
       in multiple lines, this will all be put together in /
       the alert!");

 

I'm assuming this is for javascript and not for php, since it's in the javascript forum.  Tecnically you shouldn't use $ at the start of a variable name in javascript--the $ is reserved for machine-generated code.  I know you *can* but you shouldn't.  My guess is that you're doing it for the same reasons I used to: to bridge a gap between php and javascript when working with AJAX.  Instead, I recommend headlessCamelCase--here's a good explanation as to why:

http://www.bennadel.com/blog/1760-Five-Months-Without-Hungarian-Notation-And-I-m-Loving-It.htm

 

And don't forget to follow the specific headlessCamelCase link :wink:

http://www.dustypixels.com/downloads/headless_camel_case_its_a_beautiful_thing.jpg

 

 

 

Anywho, your code would probably be more stable in the long term if you set the variable value first (and imho, it's easier to read and see what's going on...)

queryIdValue = idValue++;  //Same as idValue + 1;
var sql = "UPDATE answers SET someid= "+queryIdValue /
                       +"WHERE something = "+somethingValue;

Link to comment
Share on other sites

I'm such an idiot, I meant to put this in the MySQL forum.  Ugh, I'm having a bad day.  And sorry to make you write all that because of my idiotic mistake.

Can someone with the ability to move this, please do so. I don't see the "move" option.

I think I need to just call it day at this rate :)

Link to comment
Share on other sites

Well, then I'm assuming that's a php variable you're using for $sql.

 

In that case, you're missing the $ before someid.

 

Although I'd still set the value before running the query.  And you'd be better of doing it the same way, just using php notation. 

 

$queryIdValue = idValue++;  //Same as idValue + 1;var 
$sql = "UPDATE answers SET someid= '$queryIdValue' WHERE something = '$somethingValue'";
$result = mysql_query($sql);

Always works for me.

 

 

Link to comment
Share on other sites

You're probalby right, since OP said it should be in the MySQL forum.  I'm just going by the $ preceeding a variable name.

:shrug:

 

Regardless, I should point out an alteration in my above example:  The single quotes around the variable shouldn't be needed when the variable doesn't represent a string. 

Link to comment
Share on other sites

Don't fret, I'm solely to blame for any confusion in the post  :)

I noticed Pikachu used backticks.  I knew they were necessary if ever you used a "reserved" word in your queries, but is it good practice to ALWAYS use backticks (whether the word is reserved or not)?

Link to comment
Share on other sites

I agree with Pikachu... backticks??..... depend to whom you ask.... in my case... I will say... DEPEND... if you are designing/programming ONLY for MYSQL worth to be in the safe side and use the backticks... but if you are programing to eventually point to a different DB (different SQL) then backticks will be a PITA... personally I never use them and apply extra caution to choose my tables and field names.

 

just my 0.2cents

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.