Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Incrementing Mysql Integer, int comes out as string?
elrohir
post Dec 20 2006, 09:16 PM
Post #1


Premium Member
********

Group: Members
Posts: 165
Joined: 17-May 06
Member No.: 23,815



I am attempting to perform mathematical operations on a number extracted from a MySQL database. The integer is stored as an integer in the database, and nothing I am doing to it in the PHP script would transform it into a floating-point value.
I don't even want to worry about sticking it back in just yet:

CODE

// the value I want to increase here is logins...
$query1 = "SELECT * FROM users WHERE id='$id'";
$result1 = mysql_query($query1) or die(mysql_error());
$row1 = mysql_fetch_array($result1);

$new = $row['logins'] ++;
echo $new;


returns:

[code][/code]

aka, nothing. NULL.

It's like I'm trying to add something to a string :/ Any ideas?

Thanks,
-E
Go to the top of the page
 
+Quote Post
jlhaslip
post Dec 20 2006, 11:45 PM
Post #2


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,083
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



Untested.
CODE

$new += $row['logins'];

Or:
CODE

$new = $row['logins'] + 1;
Go to the top of the page
 
+Quote Post
elrohir
post Dec 20 2006, 11:49 PM
Post #3


Premium Member
********

Group: Members
Posts: 165
Joined: 17-May 06
Member No.: 23,815



yeh... it worked after 5 refreshes, for some reason :/

but now it won't update...

CODE

$query ="UPDATE users SET logins='$new' WHERE id='$id'";
// echo $id returns the correct number.


?
-E
Go to the top of the page
 
+Quote Post
midnitesun
post Dec 20 2006, 11:50 PM
Post #4


Premium Member
********

Group: Members
Posts: 181
Joined: 22-February 06
Member No.: 19,007



now i am really confused , from where is $row coming ? you have given $row1 as the mysql fetch array so the code should i believe be $row1['logins'] instead of $row['logins'] , it seems you are trying to display all logins as an array , so if i am correct you can use
CODE

while $row1 = mysql_fetch_array($result1)
{
$new = $row1['logins'];
echo $new;

}


also try avoiding numbers in your code, it can be confusing both for you and your code
Go to the top of the page
 
+Quote Post
jlhaslip
post Dec 20 2006, 11:55 PM
Post #5


A computer once beat me at chess, but it was no match for me at kick boxing.
Group Icon

Group: [MODERATOR]
Posts: 4,083
Joined: 24-July 05
From: Linix, DOS and Windows…the good, the bad and the ugly
Member No.: 9,787
Spam Patrol



Good catch, Midnitesun.
Go to the top of the page
 
+Quote Post
elrohir
post Dec 20 2006, 11:59 PM
Post #6


Premium Member
********

Group: Members
Posts: 165
Joined: 17-May 06
Member No.: 23,815



that should do the trick... -.-

would have thought that it would not matter if there was only one record matching the WHERE...

I'll give that a go biggrin.gif
Go to the top of the page
 
+Quote Post
electron
post Dec 22 2006, 05:45 PM
Post #7


Premium Member
********

Group: Members
Posts: 162
Joined: 10-May 06
Member No.: 23,375



Well you mentioned you wantd to try to convert it to float.
To do that just put '(float)' in front of the Variable.
You can even convert it to a string using (string).
Also instead of using numbers in variables use arrays as that can simplify the whole process and there are many functions in PHP that are useful to be applied to arrays.

Hope this helps
Go to the top of the page
 
+Quote Post
elrohir
post Dec 22 2006, 05:51 PM
Post #8


Premium Member
********

Group: Members
Posts: 165
Joined: 17-May 06
Member No.: 23,815



well, it wasn't exacly the most important number, so I just set the default in the database to 1 instead. I think it considered it 0 as in NULL, cause that solved it blink.gif

thanks anyways biggrin.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Mysql Database Setup : Setting Up Mysql Database(6)
  2. Complete Login System(57)
  3. Increment A Mysql Column(7)
  4. Import From Excel File Into Mysql Database(11)
  5. Mysql Database Size(7)
  6. Subquery In Mysql(5)
  7. Quick Question(10)
  8. Check Referrer To Prevent Linking Yours From Other Sites(8)
  9. Is It Possible To Access The Mysql Remotely?(10)
  10. Qupis : Free Hosting With Php, Mysql, Cpanel. (one Line Text Ad At Bottom)(41)
  11. Data Structures -- String -- Palindrome(5)
  12. Mysql Won't Update(4)
  13. Tools Needed!(9)
  14. Database With Mysql++(7)
  15. Mysql-essential-5.0.51 Installion Problem(2)
  1. Mysql, Php, Apache Downloads And Setup Was A Nightmare(10)
  2. Html Form!(4)
  3. Create Table - Mysql Code - Help(1)
  4. Remote Access Mysql(8)
  5. Please Explain Me Mysql(4)
  6. How Do I Find My Mysql Password? [resolved](5)
  7. It Went Well For A While(5)
  8. Mysql Db Is Down Getting Following Error(2)
  9. Need Advice On Setting Up Mysql Database.(4)
  10. The Only Reason I Choose Ms Sql Server Rather Than Mysql(0)
  11. Mysql Database Not Working(3)
  12. Creating A Php Login System Using Mysql(10)
  13. Mysql Hacks(1)