...

View Full Version : Adding one to a variable



treeleaf20
01-12-2010, 03:29 PM
All,
I have the following code and it should work but it isn't:

$qrynum = "Select MAX(picture_id) as picture_id from pictures";
$resultnum = mysql_query($qrynum);
$resultsetnum = mysql_fetch_array($resultnum);
echo $resultsetnum['picture_id'];
$newnum = $resultsetnum['picture_id']+1;


Any idea what I'm doing wrong?

JAY6390
01-12-2010, 03:36 PM
You should use the WWW rule here
What did you try, What did you get, What did you expect
You need to provide more info. What is the echo of $resultsetnum['picture_id'] returning?

treeleaf20
01-12-2010, 03:38 PM
I tried this:

$qrynum = "Select MAX(picture_id) as picture_id from pictures";
$resultnum = mysql_query($qrynum);
$resultsetnum = mysql_fetch_array($resultnum);
echo $resultsetnum['picture_id'];
$newnum = $resultsetnum['picture_id']+1;
$filename= "gallery-img$newnum.jpg";
echo $filename;


I also tried:

$qrynum = "Select MAX(picture_id) as picture_id from pictures";
$resultnum = mysql_query($qrynum);
$resultsetnum = mysql_fetch_array($resultnum);
echo $resultsetnum['picture_id'];
$newnum = $resultsetnum['picture_id']++;
$filename= "gallery-img$newnum.jpg";
echo $filename;

Both of these outputs produced:
16
gallery-img16.jpg

It correctly pulls the max number, but it doesn't update it.

JAY6390
01-12-2010, 03:40 PM
Oh so you want to update the value in the database? If that's the case, you need to run a query on the DB to do this

treeleaf20
01-12-2010, 03:42 PM
No, I do update the update the database but I actually use the filename to upload a file and then update the db with that value. So I need to get the max picture_id (the picture_id and the filename are the same) from the datbase and then increment it by one so I don't have the same filename.

JAY6390
01-12-2010, 03:47 PM
hmm I see. Can you do the following both before and after the update is meant to happen, and show the output

var_dump($resultsetnum['picture_id']);Also if you were trying

$newnum = $resultsetnum['picture_id']++;you should have been using

$newnum = ++$resultsetnum['picture_id'];

oesxyl
01-12-2010, 08:40 PM
Also if you were trying

$newnum = $resultsetnum['picture_id']++;you should have been using

$newnum = ++$resultsetnum['picture_id'];
there is no difference in this context, :)

best regards

JAY6390
01-12-2010, 08:48 PM
Yes there is watch...
Run this

$a = 5;
$b = $a++;
echo $b.'<br />';

$a = 5;
$b = ++$a;
echo $b.'<br />';
The same code twice with only the ++ difference and yet the output is 5 for the first example, and 6 for the second. The reason is that $a only gets incremented AFTER the line has executed and assigned 5 to b in the first. It does it before the line executes in the second so it sets b to 6...

oesxyl
01-12-2010, 09:07 PM
Yes there is watch...
Run this

$a = 5;
$b = $a++;
echo $b.'<br />';

$a = 5;
$b = ++$a;
echo $b.'<br />';
The same code twice with only the ++ difference and yet the output is 5 for the first example, and 6 for the second. The reason is that $a only gets incremented AFTER the line has executed and assigned 5 to b in the first. It does it before the line executes in the second so it sets b to 6...
yes, you are right, :)

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum