Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 27th, 2003, 04:23 PM
Authorized User
 
Join Date: Jun 2003
Location: San Jose, CA, USA.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Manual Increment MySQL Field

I'm trying to put a counter field into numerous MySQL tables in order to track clicks/views and other things. I want to try and make the update as easy as possible. Will this query do the trick? Is there a cleaner way to do this?

Assume current contents of counter field is 0 result should be 1? right?

UPDATE subjects SET counter = counter + 1 WHERE id = 512;

----------
~cmiller
__________________
----------
~cmiller
Reply With Quote
  #2 (permalink)  
Old August 27th, 2003, 04:53 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The simple answer is "Yes, that query will increment the counter field column of the subjects table in all rows where the id column is 512."

The longer answer is "Well, that depends on what you mean 'result'."

In MySQL, the return value of an UPDATE query is the number of rows updated. If your query executes without any problems, but the WHERE clause doesn't match any rows, the return value will be zero, which many people incorrectly interpret as "false" or "error".

In PHP, this "return value" is the number returned by calling mysql_affected_rows().

Another common mistake is calling mysql_num_rows() instead. mysql_num_rows() returns the number of rows in the result set of a SELECT query. mysql_affected_rows() returns the number of rows affected by DELETE, INSERT, REPLACE, or UPDATE queries.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #3 (permalink)  
Old August 27th, 2003, 04:55 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

One more thing -- as always, it helps to scan through appropriate manuals when looking for answers.

I don't mean to sound pompous, Chris, but it would probably take less time to figure out the answer by writing a simple test table and test query than it would to wait for a reply.

Also, this link in the MySQL manual describes the order of operations in an UPDATE query and lists a very similar query to the one you'd like to perform:
  http://www.mysql.com/doc/en/UPDATE.html

When looking for items in the MySQL manual, I usually use google's site search instead of the search engine built-in to mysql's site.


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
  #4 (permalink)  
Old August 27th, 2003, 05:09 PM
Authorized User
 
Join Date: Jun 2003
Location: San Jose, CA, USA.
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the info Nik.

#1, I didn't mean the whole result thing as a "what I get back from the mysql command", I just mean that the database would now have this as a "result". Wasn't trying to be quite as technical as you went into there.

#2, I also know that sometimes it is easier to setup a test table and test script and do that, but at the same time, I don't always have time to breathe no-less explore the possibilities of trying something new to save time on a project I'm working on now.

#3, You said it yourself that the MySQL search on their site sucks so I rarely find anything helpful, but I will try your suggestion of checking google for it first.

Lastly, the main reason I put these up here is just because I find something that works for me, doesn't necessarily mean that it's right or efficient as I have found out in the past. I value the knowledge of the people on this board thus trust your answers more than me just guessing. As I'm sure you've endountered before, just because it works doesn't mean it's right.

Thanks for your time!

----------
~cmiller
Reply With Quote
  #5 (permalink)  
Old August 27th, 2003, 05:26 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Touche! You're definitely right about your last main reason. I can't begin to tell you how many people post code snippets I saw that do the right thing the wrong way.

Not that you'd ever be in that group...!


Take care,

Nik
http://www.bigaction.org/
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto Increment Field Problem in SQL Server 2000 yes_no ASP.NET 1.0 and 1.1 Professional 4 August 11th, 2008 07:10 AM
Linux: uninstalling MySQL by manual file deletion ninsat MySQL 1 March 16th, 2005 12:42 PM
SQL Server Identity field won't increment Ron Howerton VB.NET 2002/2003 Basics 8 April 22nd, 2004 12:14 PM
MySQL AUTO_INCREMENT field fcamilo Beginning PHP 1 August 21st, 2003 06:29 PM
How can I change a field name in mysql? scifo Beginning PHP 2 August 1st, 2003 09:41 AM



All times are GMT -4. The time now is 03:55 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.