Adding +1 to value from database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SL20
Forum Newbie
Posts: 2
Joined: Mon Sep 14, 2009 6:56 am

Adding +1 to value from database

Post by SL20 »

Hi all,

I am fairly new to PHP. I have written some code that adds 1 to the highest value in a database and stores it in a variable.

Code: Select all

$query = mysql_query("SELECT MAX(id) as maxnum FROM users");
$id = mysql_fetch_array($query);
$id = $id[maxnum]+1;
The code does work, but I am not sure if:

A) I have done this correctly, or if there is a better/easier way to do this?
B) Why I can't say: $id = $id[maxnum]++; ?
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Re: Adding +1 to value from database

Post by mattpointblank »

What are you doing this for? If it's so you can 'guess' the ID of a row you just inserted, there's a function for this: mysql_insert_id (http://us2.php.net/manual/en/function.m ... ert-id.php) - it gets the ID of the last row inserted by a query.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: Adding +1 to value from database

Post by jazz090 »

Code: Select all

<?php
$query = mysql_query("SELECT MAX(id) FROM users AS max_id");
$max_id = mysql_fetch_assoc($query);
mysql_query("UPDATE users SET id = id+1 WHERE id = ".$max_id['max_id']);
?>
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Adding +1 to value from database

Post by Ollie Saunders »

You haven't retrieved the id with this:

Code: Select all

$id = mysql_fetch_array($query);
You want:

Code: Select all

$id = mysql_fetch_object($query)->maxnum;
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Adding +1 to value from database

Post by Ollie Saunders »

The value retrieved from the database is a string. The increment operator does not work on strings.

Code: Select all

php > $a = '1';
php > echo ++$a;
2
Also note:

Code: Select all

php > $a = 'a';
php > echo ++$a;
b
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: Adding +1 to value from database

Post by Ollie Saunders »

I hate being wrong, but, of course, I am.
I love being wrong these days; at least, about programming, I do. It means that just I learnt something.
Who/what said you can't?
The belief that $id was holding an id. It was actually holding an array and you can't increment that:

Code: Select all

php > $a = array(1);
php > var_dump($a);
array(1) {
  [0]=>
  int(1)
}
php > var_dump(++$a);
array(1) {
  [0]=>
  int(1)
}
 
ricehigh
Forum Newbie
Posts: 21
Joined: Mon Sep 14, 2009 5:18 pm

Re: Adding +1 to value from database

Post by ricehigh »

Also, you could do the math operation directly in you SQL statement:

Code: Select all

<?php
$query = mysql_query("SELECT MAX(id)+1 as maxnum FROM absence_db");
$id = mysql_fetch_object($query)->maxnum;
?>
Post Reply